Real time plot slows down due to hold on command

조회 수: 17 (최근 30일)
Frederik Knudsen
Frederik Knudsen 2018년 3월 27일
댓글: Steven Lord 2018년 3월 27일
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

채택된 답변

Rik
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
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 CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by