MATLAB: When is the StartFcn executed when I use STARTAT with the TIMER object in MATLAB 7.6 (R2008a)

MATLAB

I create a timer with a starting function 'StartFcn' and the timer function 'TimerFcn' and start the timer with a 3 second delay using a STARTAT function:
function simpletimer
disp('Beginning of the simple timer function')
function starttimer(varargin)
disp('Executing StartFcn');
end
function testtimer(varargin)
disp('Executing TimerFcn');
end
t = timer('TimerFcn', @testtimer, 'StartFcn', @starttimer);
startat(t,now+3/24/60/60);
disp('End of simple timer function')
end
I expect the following sequence of the messages displayed:
Beginning of the simple timer function
End of simple timer function
Executing StartFcn
Executing TimerFcn
Instead, I get the following sequence of outputs:
Beginning of the simple timer function
Executing StartFcn
End of simple timer function
Executing TimerFcn

Best Answer

  • STARTAT function is used to delay the execution of the TimerFcn. When the line with the STARTAT command is reached, the following sequence of events occurs:
    1. The timer object's property 'StartDelay' is set to the difference between the second argument of STARTAT function and 'now'. In this example, the 'StartDelay' property will be set to 3 seconds. 'StartDelay' property of a timer object specifies the delay, in seconds, between the start of the timer and the first execution of the function specified in TimerFcn.
    2. StartFcn is executed. In this example, the 'Executing StartFcn' line appears in the command prompt.
    3. The TimerFcn execution is delayed until the 'StartDelay' time has elapsed. At this moment the code proceeds to execute further, displays 'End of simple timer function' and returns control to the command line. When the amount of time equal to 'StartDelay' has elapsed, the TimerFcn is executed and the 'Executing TimerFcn' message is displayed.
    For an overview of the Timer object callback functions please refer to the documentation by executing the following in the MATLAB command prompt:
    web([docroot '/techdoc/matlab_prog/f9-39541.html#f9-42494'])