1

The program below contains a timer object definition followed by its start command. Then the program continues executing other statements.

My question is whether TimerFcn will be called exactly after 0.01 sec, or will it wait until the for-loop completes for the timer callback function to fire?

% My timer object  
t = timer('TimerFcn',@(x,y)G2(z), 'StartDelay',0.01);
start(t);

% Other program statements 
for i=1:m
    ...
end
Amro
  • 123,847
  • 25
  • 243
  • 454
  • I'm going to refer you to [this question](http://www.mathworks.com/matlabcentral/answers/10394-timer-and-interruptible-off-button-callback-priority-preemption) on MATLAB Answers. Also there was a question the other day here on SO which I think is related: http://stackoverflow.com/q/24368424/97160 – Amro Jul 07 '14 at 11:25

1 Answers1

0

Bottom line is that MATLAB is effectively single-threaded. So if a long operation is currently executing, the timer callback will not get a chance to run, and according to the timer object properties (read about BusyMode), will instead add the event to a queue which MATLAB will eventually go through when it first gets a chance..

From what I understand (this is my own speculation), MATLAB timers can interrupt execution in between statements, but NOT during a lengthy one.

So in theory it should run after 0.01 second, but there are no guarantees...


The documentation says the following:

Note: The specified execution time and the actual execution of a timer can vary because timer objects work in the MATLAB single-threaded execution environment. The length of this time lag is dependent on what other processing MATLAB is performing. To force the execution of the callback functions in the event queue, include a call to the drawnow function in your code. The drawnow function flushes the event queue.

There is also this note on another doc page:

Note: Callback function execution might be delayed if the callback involves a CPU-intensive task such as updating a figure.

Amro
  • 123,847
  • 25
  • 243
  • 454