Pause(n) vs timer. Which is better?
Show older comments
So I'm trying to show a plot of time dependent data as if it is taken in real time. The issue with using the pause(n) function within a for-loop is that the loop performance deteriorates, and plotting the functions drastically slows down. Can this be remedied with a timer? And how could I achieve the same outcome as the code below using a timer?
Here's the code:
% inputs
t = 1:0.05:25;
% time-dependent functions
sin_t = sin(t);
cos_t = cos(t);
% loop for time-dependent measurements
n = numel(t)
figure, xlim([min(t) max(t)]), ylim([-2 2]);
hold on
for i = 1:n
plot (t(1:i), sin_t(1:i));
plot (t(1:i), cos_t(1:i));
pause(0.01);
end
hold off
2 Comments
you could offcourse use timers, but you could also gain something on not replotting the same data.
you could for instance try with:
for i=1:n-1
plot( t(1+i-1:i+1) , sin_t(1+i-1:i+1) )
...
then you wont use unnecessary time plotting stuff that is already in your plot.
Sean de Wolski
on 26 Jul 2013
@kjetil: an even bigger improvement is to not call PLOT again. Calling plot requires creating an additional line which has its own overhead. It's more efficient to change the data of the existing line object like I did below. NOTE, you could do this in a for-loop as well as in the timer callback.
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!