Efficient online plot?

조회 수: 1 (최근 30일)
grega
grega 2018년 12월 27일
댓글: grega 2018년 12월 31일
I wonder why Matlab memory usage increases with this simple code updating the plot recursively.
% subplot initialization
figure,subplot 311,ha{1} = plot(rand(100,1));
subplot 312,ha{2} = plot(rand(100,1));
subplot 313,ha{3} = plot(rand(100,1));
% recursive calc and update plot
for i = 1 : N % some long recursive calculation
Y = rand(100,3); % some calculation or simply rand(100,3) applied gives the same problem; % Y was pre-allocated and is kept of the same size; where 3 corresponds to 3 lines
plot_update(ha,Y)
memory % measuring the memory consumed by Matlab
end
% --- func for updating plot
function plot_update(ha,Y)
for i = 1 : length(ha)
set(ha(i),'LineJoin','round','YData', Y(:,i)); %hold on; % 'auto y';
end
So, the Y matrix is of constant size and used to update YData of the plot at every step of the calculation. Looking at the Matlab's memory usage, it slowely but steadily increases with time and I cannot figure it out. Thank you on any comments how to improve the code.
  댓글 수: 1
Jan
Jan 2018년 12월 28일
Just a hint, but not the problem: You define ha as a cell. It works to provide the handle as a scalar cell, but it might be more clear to use an array of handles:
ha(1) = plot(rand(100,1)); % Instead of curly braces {1}

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

답변 (1개)

Jan
Jan 2018년 12월 28일
편집: Jan 2018년 12월 28일
What is the problem you want to solve? Matlab creates new data in each iteration by:
Y = rand(100,3);
The former values are not needed anymore, but a garbage collection would need some time. Therefore it is possible that Matlab waits until collecting free memory is required and until then is seems like the used memory is growing, but it is only the part, which has not been declared as free. So I assume, that there is no problem at all.
By the way, the subplot 131 style is outdated since Matlab 5.3 - for 20 years now. It is time to use the modern style subplot(1,3,1).
  댓글 수: 1
grega
grega 2018년 12월 31일
Thanks on the feedback. To demonstrate what I mean I prepared the code (attached). If you compile the code
mcc -e MyTimer.m -o MyTimer
and run in, the problem is even more nicely demonstrated. The memory of the compiled code increases over time until the PC's memory is eventually full (It'd take a lot of time, but still). With help of the "memory" and "whos" functions I learned that the variables sizes in terms of memory keep constant (except the scalar counter) and that the memory increases due to the plot. Why plotting is the reason and how to improve it?

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by