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