Azzera filtri
Azzera filtri

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

29 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Chris
Chris il 28 Ott 2021
Modificato: Chris il 28 Ott 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 Commenti
Joshua Knicely
Joshua Knicely il 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 il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by