Efficient online plot?
1 次查看(过去 30 天)
显示 更早的评论
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
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
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).
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!