Real time plot slows down due to hold on command
4 次查看(过去 30 天)
显示 更早的评论
Hey, I have been trying to make a real-time plot to visualize some data which works fine except the plot severely slows down after about 40 seconds and also keeps using more and more RAM.
data = zeros(200,1); %Preallocate memory
i = 0;
time = [0.0001:.0001:0.02]';
figure(1);
hold on;
while 1
data = sin(time)*250+250;
time = [max(time)+.0001:.0001:max(time)+0.02]'; %Increase time interval by 20 ms
plot(time,data); %Plot the newest 200 samples
axis([min(time)-3 max(time), 0 , 1000]); %Plot the last three seconds
grid on;
if ~mod(i,8)*i/8>0 %Draw plot every 8th iteration to speed up plot
drawnow;
end
if i >= 4096 %Clear every 4096th iteration to save memory
i = 0;
end
i = i + 1;
end
When disabling the hold on command, the plot doesn't slow down but i of course only get the newest 200 samples. Since I don't really need to inspect more than ten seconds of data, is there anyway I can get around using the hold on command? I saw someone talking about using handles to only keep some of the plots but I don't know how to use those.
Thanks
0 个评论
采纳的回答
Rik
2018-3-27
You are adding many lines to a plot, of course at some point that will slow down your computer. A solution would indeed be to have a maximum number of plots (e.g. store the output of plot in an array and use try to delete the i element (with the delete function)). You can also use cla to clear the axis after some number of iterations.
1 个评论
Steven Lord
2018-3-27
Alternately, using set and get to update the XData and YData properties of the line whose handle is the output argument of plot or using an animatedline would allow you to avoid creating one new line per iteration.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!