Updating plot in loop takes increasing amount of time

조회 수: 4 (최근 30일)
dleal
dleal 2022년 5월 5일
댓글: dleal 2022년 5월 6일
When I update a plot within a loop, each iteration takes longer after a while. Is there a way to avoid the slowdown? See example below:
f = figure;
a = axes(f);
hold(a,"on")
% note: y is supposed to represent real-time data that I receive with some
% frequency. The full vector Y is not available right before plotting
y = randn(1,100000);
for jj = 1:numel(y)
tt = tic;
plot(a,jj,y(jj),'pg');
drawnow;
pause(0.02)
toc(tt)
end
I understand the x and y arguments of the plot are getting larger. Is that the cause? After 6,000 iterations, it already takes three times as long as when the loop it started. Thank you
  댓글 수: 2
Matt J
Matt J 2022년 5월 5일
편집: Matt J 2022년 5월 5일
I assume there's a reason that you don't simply do this in a single plot command?
f = figure;
a = axes(f);
plot(a,y,'pg')
dleal
dleal 2022년 5월 5일
Hi Matt,
yes, I should have clarified. "y" is supposed to represent real time data that I poll with some frequency. So the full array is not available before plotting

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 5월 6일
Use animatedline()
  댓글 수: 1
dleal
dleal 2022년 5월 6일
Hi Walter, thanks! this is perfect. After 100,000 iterations, updating the animated line takes about the same for the first few updates as for the last ones. This works just great.

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

추가 답변 (1개)

Matt J
Matt J 2022년 5월 5일
편집: Matt J 2022년 5월 5일
I understand the x and y arguments of the plot are getting larger. Is that the cause?
The arguments aren't getting larger, but the number of objects in the plot is getting larger and larger. You can reduce the overall time, though, if the graph doesn't need to be redrawn as frequently.
Maybe update in sub-chunks of y:
f = figure;
a = axes(f);
hold(a,"on")
y = randn(1,100000);
for jj = reshape( 1:numel(y),1000,[]) %<--- loop over sub-chunks of length 1000
tt = tic;
plot(a,jj,y(jj),'pg');
drawnow;
pause(0.02)
toc(tt)
end
  댓글 수: 1
dleal
dleal 2022년 5월 6일
thank you Matt! I did some additional tests and verified that drawnow takes a very very long time

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

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by