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

4 次查看(过去 30 天)
  3 个评论
dpb
dpb 2022-6-26
编辑:dpb 2022-6-26
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 2022-6-26
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.

请先登录,再进行评论。

回答(2 个)

Voss
Voss 2022-6-26
编辑:Voss 2022-6-26
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 2022-6-26
编辑:DGM 2022-6-26
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.

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by