Why won't my plot graph a line?
1 view (last 30 days)
Show older comments
CHase Palmer
on 2 May 2016
Commented: John D'Errico
on 2 May 2016
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 Comments
John D'Errico
on 2 May 2016
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.
Accepted Answer
John D'Errico
on 2 May 2016
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.
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!