Programming and Data Types    

Timer Object Execution Modes

The timer object supports several execution modes that determine how the timer object schedules the timer callback function (TimerFcn) for execution. You specify the execution mode by setting the value of the ExecutionMode property. The execution modes fall into two categories:

Executing a Timer Callback Function Once

To execute a timer callback function once, set the ExecutionMode property to 'singleShot'. This is the default execution mode.

In this mode, the timer object starts the timer and, after the time period specified in the StartDelay property elapses, adds the timer callback function (TimerFcn) to the MATLAB execution queue. When the timer callback function finishes, the timer stops.

Executing a Timer Callback Function Multiple Times

The timer object supports three multiple-execution modes:

In many ways, all of these execution modes operate the same:

The execution modes differ only in where they start measuring the time period between executions. In 'fixedRate' mode, the timer starts measuring this time period immediately after adding the timer callback function (TimerFcn) to the MATLAB execution queue. In 'fixedDelay' mode, the timer starts measuring this time period when the timer function callback actually starts executing, after any time lag due to delays in the MATLAB execution queue. In 'fixedSpacing' mode, the timer starts measuring this time period, from the point when the timer callback function finishes executing.

This figure illustrates the difference between these modes.

Handling Callback Function Queuing Conflicts

At busy times, in multiple execution scenarios, the timer may need to add the timer callback function (TimerFcn) to the MATLAB execution queue before the previously queued execution of the callback function has completed. You can determine how the timer object handles this scenario by using the BusyMode property.

If you specify 'drop' as the value of the BusyMode property, the timer object skips this execution of the timer function callback.

If you specify 'queue', the timer object waits until the currently executing callback function finishes before queuing the next execution of the timer callback function.

If the BusyMode property is set to 'error', the timer stops and executes the timer error function (ErrorFcn).


  Starting and Stopping Timers Creating Timer Callback Functions