| MATLAB® | ![]() |
| On this page… |
|---|
Executing a Timer Callback Function Once |
The timer object supports several execution modes that determine how it schedules the timer callback function (TimerFcn) for execution. You specify the execution mode by setting the value of the ExecutionMode property.
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.
The following figure graphically illustrates the parts of timer callback execution for a singleShot execution mode. The shaded area in the figure, labelled queue lag, represents the indeterminate amount of time between when the timer adds a timer callback function to the MATLAB execution queue and when the function starts executing. The duration of this lag is dependent on what other processing MATLAB happens to be doing at the time.
Timer Callback Execution (singleShot Execution Mode)

The timer object supports three multiple-execution modes:
'fixedRate'
'fixedDelay'
'fixedSpacing'
In many ways, these execution modes operate the same:
The TasksToExecute property specifies the number of times you want the timer to execute the timer callback function (TimerFcn).
The Period property specifies the amount of time between executions of the timer callback function.
The BusyMode property specifies how the timer object handles queuing of the timer callback function when the previous execution of the callback function has not completed. See Handling Callback Function Queuing Conflicts for more information.
The execution modes differ only in where they start measuring the time period between executions. The following table describes these differences.
Execution Mode | Description |
|---|---|
'fixedRate' | Time period between executions begins immediately after the timer callback function is added to the MATLAB execution queue. |
'fixedDelay' | Time period between executions begins when the timer function callback actually starts executing, after any time lag due to delays in the MATLAB execution queue. |
'fixedSpacing' | Time period between executions begins when the timer callback function finishes executing. |
The following figure illustrates the difference between these modes. Note that the amount of time between executions (specified by the Period property) remains the same. Only the point at which execution begins is different.
Differences Between Execution Modes

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 the execution of the timer function callback if the previously scheduled callback function has not already completed.
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.
Note In 'queue' mode, the timer object tries to make the average time between executions equal the amount of time specified in the Period property. If the timer object has to wait longer than the time specified in the Period property between executions of the timer function callback, it shortens the time period for subsequent executions to make up the time. |
If the BusyMode property is set to 'error', the timer object stops and executes the timer object error callback function (ErrorFcn), if one is specified.
![]() | Creating and Executing Callback Functions | Deleting Timer Objects from Memory | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |