Why won't my plot graph a line?

조회 수: 2 (최근 30일)
CHase Palmer
CHase Palmer 2016년 5월 2일
댓글: John D'Errico 2016년 5월 2일
for r = 1:rows
hold on
%THIS IS THE PLOT THAT WON'T CONNECT EACH POINT. IT ONLY DISPLAYS THE 'b*'
plot (averageline(2,r),averageline(1,r),'b*',averageline(2,r),averageline(1,r),'b-')
fplot(@(x)[7*sin(x)-3*x^2+11*cos(x^3)+47*x-25],[0,2],'r')
end
legend('Raw Data','Point Marker','Math Expression')
%for r = 1:rows
  댓글 수: 3
CHase Palmer
CHase Palmer 2016년 5월 2일
Are my functions needed?
John D'Errico
John D'Errico 2016년 5월 2일
No. I don't think I need to see your functions. Your problem here was just that the call to plot needed to be one single call with vector arguments. MATLAB is naturally a vector/array language. Once you get used to working with vectors and arrays directly instead of loops, you will find your code becomes easier to read and write. It will work faster. Lots of gain there as you become more experienced in the language.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2016년 5월 2일
Note that IF you want to plot a BLUE line connecting the points, with * symbols at the data points, then it is sufficient to call plot as:
plot(x,y,'b*-')
For example,
plot(rand(1,10),rand(1,10),'b*-')
Ok, so that is a minor issue, sort of. But the real reason why your plot fails is every call that you make to plot, you are just plotting a SINGLE point.
Instead, drop the loop! Call plot ONCE.
plot(averageline(2,:),averageline(1,:),'b*-')
Or, you can do the plot as you did originally, as effectively two separate "curves".
plot(averageline(2,:),averageline(1,:),'b*',averageline(2,:),averageline(1,:),'b-')
By passing in an entire vector of points, plot will now do as you wish. You can then use hold to add the function plot.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by