Efficient online plot?

1 visualizzazione (ultimi 30 giorni)
grega
grega il 27 Dic 2018
Commentato: grega il 31 Dic 2018
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 Commento
Jan
Jan il 28 Dic 2018
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}

Accedi per commentare.

Risposte (1)

Jan
Jan il 28 Dic 2018
Modificato: Jan il 28 Dic 2018
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 Commento
grega
grega il 31 Dic 2018
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?

Accedi per commentare.

Categorie

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

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by