Filter löschen
Filter löschen

How do I get the plot in my for loop to retain previous plots?

28 Ansichten (letzte 30 Tage)
I'm trying to set up a for loop inside a function to plot my data so I can see it evolving. Here's the relevant code. My function updates 'T'; z is just a vector of the same length.
% Plot every hundredth iteration; this is so I can see if it's changing.
c = c + 1;
if c == 100
c = 0;
fig = figure(1);
axh = axes;
plot(axh,T,z)
hold all
grid on
set(gca,'YDir','Reverse')
title('Evolving Geotherm')
xlabel('Temperature (K)'); ylabel('Depth (m)')
end
This will display ONLY the latest data; none of the previous plots remain. I get the same results when I use 'hold on' and 'drawnow'. This seems like a really simple bit of code.
What's going on here? Why won't the previous plots remain?

Akzeptierte Antwort

Chris
Chris am 28 Okt. 2021
Bearbeitet: Chris am 28 Okt. 2021
You are regenerating the figure and axes every time the if condition triggers. About the only thing you need inside the if is the plot() command. The other stuff should go before the for loop (but inside the function).
fig = figure(1);
axh = axes
hold all
grid on
set(gca,'YDir','Reverse')
title('Evolving Geotherm')
xlabel('Temperature (K)'); ylabel('Depth (m)')
c = 0;
for idx = 1:whatever
% Do some loop stuff...
c = c+1;
if c == 100
c = 0;
plot(axh,T,z)
end
end
  6 Kommentare
Joshua Knicely
Joshua Knicely am 3 Nov. 2021
@Chris & @Jeff Miller, y'all were correct about the drawnow. With that, it shows my plot as the data changes.
@Chris, not sure about meaning with the function scope. Would that be like an accidental recursive call of the function on itself? My data is updated in the for loop.
Chris
Chris am 3 Nov. 2021
I was searching for other reasons it could be failing. If the function were calculating one point each time it's called, over repeated calls, it might not have access to all points to pass to plot. I was stretching my imagination a bit, since I couldn't see how the function was working.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by