Filter löschen
Filter löschen

Real time plot slows down due to hold on command

4 Ansichten (letzte 30 Tage)
Frederik Knudsen
Frederik Knudsen am 27 Mär. 2018
Kommentiert: Steven Lord am 27 Mär. 2018
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

Akzeptierte Antwort

Rik
Rik am 27 Mär. 2018
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 Kommentar
Steven Lord
Steven Lord am 27 Mär. 2018

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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by