From: Daniel on
I am having some problems with using timer objects. I thought Matlab was "single threaded" and that timer callbacks would be placed in the event queue and only executed when a drawnow (or any of the other events that flushes the queue) occured. It does not seem to work this way.

function timertest
flag = false(3, 1);
temp = 0;
T0 = clock;
start(timer('Name', 'Stupid', 'ExecutionMode', 'SingleShot', ...
'StartDelay', 0.6, 'TimerFcn', @(src, evt)disp('timer callback')));
while etime(clock, T0) < 1
if etime(clock, T0) > 0.25 && ~flag(1)
disp('loop start')
flag(1) = true;
end
if etime(clock, T0) > 0.5 && ~flag(2)
disp('loop middle')
flag(2) = true;
end
if etime(clock, T0) > 0.75 && ~flag(3)
disp('loop finish')
flag(3) = true;
end
temp = temp+1;
end
end

The output I get is:
loop start
loop middle
timer callback
loop finish

I expected the output to be:
loop start
loop middle
loop finish
timer callback


Even though the timer callback should occur at 0.6 seconds, (i.e., between middle and finish), I epxected it to patiently wait in the queue until the loop finished. What am I missing? Is something in my loop flushing the event queue, or does matlab execute timer object callbacks on a seperate thread?