Why won't my plot graph a line?

2 visualizaciones (últimos 30 días)
CHase Palmer
CHase Palmer el 2 de Mayo de 2016
Comentada: John D'Errico el 2 de Mayo de 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 comentarios
CHase Palmer
CHase Palmer el 2 de Mayo de 2016
Are my functions needed?
John D'Errico
John D'Errico el 2 de Mayo de 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.

Iniciar sesión para comentar.

Respuesta aceptada

John D'Errico
John D'Errico el 2 de Mayo de 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.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by