필터 지우기
필터 지우기

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

조회 수: 28 (최근 30일)
Joshua Knicely
Joshua Knicely 2021년 10월 28일
댓글: Chris 2021년 11월 3일
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?

채택된 답변

Chris
Chris 2021년 10월 28일
편집: Chris 2021년 10월 28일
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
Joshua Knicely
Joshua Knicely 2021년 11월 3일
@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 2021년 11월 3일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by