I have tried many different ways of plotting but it won't make a line graph can someone help me out?

1 view (last 30 days)
Donovan Sellers
Donovan Sellers on 26 Jun 2022
Commented: dpb on 26 Jun 2022
  3 Comments
dpb
dpb on 26 Jun 2022
Or, while more advanced and probably not what the lesson is trying to teach, to plot points in a loop when generated one at a time, use animatedline and addpoints.

Sign in to comment.

Answers (2)

Voss
Voss on 26 Jun 2022
Edited: Voss on 26 Jun 2022
Either:
n = 100;
for i = 1:n
x = rand();
% ...
% ...
% plot(n,x);
plot(i,x,'.'); % use a data marker to see the point,
hold on % and hold on for the next point
end
Or:
n = 100;
for i = 1:n
% x = rand();
x(i) = rand(); % collect all x values in a vector
% ...
% ...
% plot(n,x);
end
plot(1:n,x); % and plot all the points after the loop

DGM
DGM on 26 Jun 2022
Edited: DGM on 26 Jun 2022
You are creating a plot object that contains only one point, but have no specified marker style to indicate those points. There is a line style implicitly specified, but there is no line plotted because there are no lines.
There is only one plot object because each new point replaces the last one. Use hold on to include more than one graphics object in an axes -- or better, don't do the plotting in the loop.
It's a common question.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by