Matlab produces empty figure when using plot(x,y,'-')

조회 수: 3(최근 30일)
Mia DeCataldo
Mia DeCataldo 2022년 4월 1일
댓글: Mia DeCataldo 2022년 4월 2일
Hi, I have a code that works fine, its meant to plot time t over a variable A. the plot function works when i specify plot(t,A,'*'), but produces an empty figure when i use any other marker ( for example using 'o' or '-' will produce an empty figure), and when I dont specify a marker. I am on MacOS version 12.1.
example: this conde works and produces a graph
A = 20
t = 0
while t < 20
A = A + 1
t = t + 1
plot(t,A,'*')
hold on
end
however this:
A = 20
t = 0
while t < 20
A = A + 1
t = t + 5
plot(t,A,'-')
hold on
end
creats an empty figure.
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 4월 1일
'-' is not a marker: it is a plot line style.
I would, though, expect 'o' to work as a marker.

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2022년 4월 2일
편집: DGM 2022년 4월 2일
If you want your points to be connected by lines, save the data as vectors and then plot it outside the loop.
If I can assume that the given example is the task at hand, then it can be simplified such that the number of points can be known. I'm going to assume that you also want to plot the first point (0 20).
N = 5; % number of points
A = zeros(1,N);
t = zeros(1,N);
A(1) = 20; % initial conditions
t(1) = 0;
for k = 2:N
A(k) = A(k-1) + 1;
t(k) = t(k-1) + 5;
end
plot(t,A,'-o') % line and/or markers work
Of course, this case can still be simplified to avoid the loop, but that defeats the point of talking about the loop.
This may be complicated if the while loop is actually necessary. If I assume that this is a simplified example and that in practice, the code inside the loop is more complex and the number of loop iterations cannot be known, then preallocating the vectors might not be exactly possible. There are a couple things you could do.
The simple thing would be to just grow the vectors. This isn't ideal and can slow things down. Considering what's being done here, it's probably still faster than calling plot() inside the loop. For a total of 5 points, the cost is entirely negligible.
figure
A = 20; % initial conditions
t = 0;
k = 2;
while t < 20
A(k) = A(k-1) + 1;
t(k) = t(k-1) + 5;
k = k+1;
end
plot(t,A,'-o') % line and/or markers work
@Mrutyunjaya Hiremath shows another method of growing the vectors by using concatenation instead of indexing. That approach is often convenient in some circumstances, but the arrays are still growing instead of being preallocated. Again, the expense wouldn't matter if the task is actually this small.
If the vectors are going to be large, it may be worthwhile trying to find an upper boundary on the vector size, preallocate vectors that size, and then when the loop exits, truncate the excess based on what value k is.
  댓글 수: 1
Mia DeCataldo
Mia DeCataldo 2022년 4월 2일
Hi, thank you. This was very helpful.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath 2022년 4월 2일
A = 20
A = 20
Ap = A;
t = 0
t = 0
tp = t;
while t < 20
A = A + 1
Ap = [Ap, A];
t = t + 5
tp = [tp, t];
end
A = 21
t = 5
A = 22
t = 10
A = 23
t = 15
A = 24
t = 20
figure
plot(tp, Ap, '-')
  댓글 수: 1
Mia DeCataldo
Mia DeCataldo 2022년 4월 2일
thank you this worked for me!

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by