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

7 views (last 30 days)
Mia DeCataldo on 1 Apr 2022
Commented: Mia DeCataldo on 2 Apr 2022
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.
Walter Roberson on 1 Apr 2022
'-' is not a marker: it is a plot line style.
I would, though, expect 'o' to work as a marker.

DGM on 2 Apr 2022
Edited: DGM on 2 Apr 2022
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.
Mia DeCataldo on 2 Apr 2022
Hi, thank you. This was very helpful.

Mrutyunjaya Hiremath on 2 Apr 2022
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, '-') Mia DeCataldo on 2 Apr 2022
thank you this worked for me!