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

4 views (last 30 days)
  3 Comments
dpb
dpb on 26 Jun 2022
Edited: dpb on 26 Jun 2022
And you'll likely still not see anything on the plot -- certainly it won't be a line -- as you've written the code, you have only a single point each iteration and call plot with
plot(n,x)
in which n is a constant and x is that one point value...
Rethink what you're after here and what you need to draw a line...both an abscissa AND an ordinate variable each of which is a vector of points.
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.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!