pause function gives inconsistent timing when used inside a loop.

30 views (last 30 days)
Milad Omidi
Milad Omidi on 9 Aug 2019
Edited: Adam Danz on 26 Aug 2020
I am trying to create an animated plot. The pause command inside an infinite loop is supposed to created the timing for me:
figure; clf;
t = 0:1/16000:1;
y = sin(2*pi*4*t);
h = plot(t,y);
xlim([ 0 t(end)]);
ylim([ -1 1]);
h.XDataSource = 't';
h.YDataSource = 'y';
rate = 10;
while 1
%java.lang.Thread.sleep(1000 * (1/rate))
y = sin(2*pi*freq*t);
drawnow % needed?
freq = freq + 0.1;
if freq > 40
freq = 4;
When I try this tic toc shows some inconsistent timing:
Elapsed time is 0.100041 seconds.
Elapsed time is 0.599817 seconds.
Elapsed time is 0.100195 seconds.
Elapsed time is 0.100100 seconds.
Elapsed time is 0.499797 seconds.
Elapsed time is 0.100046 seconds.
If I remove everything from the loop and leave just the pause command it behaves the same.
I tried using java's sleep command but that does not allow the plots to be updated until the loop is exited which is useless.
The results shown above happens whn I run this on Linux. I tried running the same code on Windows and it works fine. Not sure what could be the reason.

Accepted Answer

Adam Danz
Adam Danz on 25 Aug 2020
Edited: Adam Danz on 26 Aug 2020
Careful using tic/toc. As summarized in the documentation, "If your code is faster than 1/10 second, consider measuring it running in a loop, and then average to find the time for a single run". Even with larger durations, there is unavoidable variability (shown in this comment within this thread).
Alternatively, use timeit to determine how long a process takes (not to control real-time processes). Unlike tic and toc, the timeit function calls your code multiple times, and, therefore, considers first-time costs.
Check out this comparison of timing pause(0.1) using toc/toc and timeit. Notice the difference in variability. For a bit more info on tic/toc timing within a loop, see this answer.
Additionally, the pause() function is susceptible to variations due to your OS and concurrent system activity. The finer the temporal resolution, the higher the relative error. For more info on limitations to the pause function, scroll down to the tips section in this link.
f = @()waitasec;
t = zeros(1,100);
for i = 1:100
t(i) = timeit(f);
t2 = zeros(1,100);
for i = 1:100
t2(i) = toc;
hold on
h = plot(t, 'DisplayName', 'timeit')
h2 = plot(t2, 'DisplayName', 'tictoc')
function waitasec
If you're trying to control the speed of animation, consider using one of the animation techniques, some of which provide examples of controlling animation speed. This blog post uses a timer object to control animation speed. I have not read or vetted the article but keep in mind that Matlab recommends not using a timer for real-time applications.

More Answers (1)

jmore on 26 Aug 2020
Edited: jmore on 26 Aug 2020
I have encountered a similar issue on my Linux operating system. Mr. Adam Danz has suggested using timeit, the the following approach is a little easier to understand for me:
rate = 10
while toc < 1/rate
This will replace to faulty pause command that varies from 0.1 to 0.5 seconds:
Adam Danz
Adam Danz on 26 Aug 2020
I must have added that after copying the code to the comment. The comment states, "Results are jittered along the x axis to prevent overlap ", otherwise many of the points would lay on top of eachother.
To jitter the points,
xJitter = (1:numel(rate))' + (rand(numel(rate),nReps)-.5)/2;
plot(xJitter, dur, 'bo', 'DisplayName', 'all iterations')
I'll fix that in my comment. Thanks for pointing that out.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!