Why won't my plot graph a line?

1 vue (au cours des 30 derniers jours)
CHase Palmer
CHase Palmer le 2 Mai 2016
Commenté : John D'Errico le 2 Mai 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 commentaires
CHase Palmer
CHase Palmer le 2 Mai 2016
Are my functions needed?
John D'Errico
John D'Errico le 2 Mai 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.

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 2 Mai 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.
  1 commentaire
CHase Palmer
CHase Palmer le 2 Mai 2016
Thanks! That worked

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by