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 Center および File ExchangeGraphics Objects についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by