| MATLAB® | ![]() |
| On this page… |
|---|
Note Because the timer works within the MATLAB single-threaded environment, it cannot guarantee execution times or execution rates. |
To start a timer object, call the start function, specifying the timer object as the only argument. The start function starts a timer object running; the amount of time the timer runs is specified in seconds in the StartDelay property.
This example creates a timer object that displays a greeting after 5 seconds elapse.
Create a timer object, specifying values for timer object properties.
t = timer('TimerFcn','disp(''Hello World!'')','StartDelay', 5);start(t)
Delete the timer object after you are finished using it.
delete(t);
To start a timer object and specify a date and time for the timer to fire, (rather than specifying the number of seconds to elapse), use the startat function. This function starts a timer object and allows you to specify the date, hour, minute, and second when you want to the timer to execute. You specify the time as a MATLAB serial date number or as a specially formatted date text string.
This example creates a timer object that displays a message after an hour has elapsed. The startat function starts the timer object running and calculates the value of the StartDelay property based on the time you specify.
t2=timer('TimerFcn','disp(''It has been an hour now.'')');
startat(t2,now+1/24);Once started, the timer object stops running if one of the following conditions apply:
The timer function callback (TimerFcn) has been executed the number of times specified in the TasksToExecute property.
An error occurred while executing a timer function callback (TimerFcn).
You can also stop a timer object by using the stop function, specifying the timer object as the only argument. The following example illustrates stopping a timer object:
t = timer('TimerFcn','disp(''Hello World!'')', ...
'StartDelay', 100);start(t)
Check the state of the timer object after starting it.
get(t,'Running') ans = on
Stop the timer using the stop command and check the state again. When a timer stops, the value of the Running property of the timer object is set to 'off'.
stop(t) get(t,'Running') ans = off
Delete the timer object when you are finished using it.
delete(t)
Note The timer object can execute a callback function that you specify when it starts or stops. See Creating and Executing Callback Functions. |
By default, when you use the start or startat function to start a timer object, the function returns control to the command line immediately. For some applications, you might prefer to block the command line until the timer fires. To do this, call the wait function right after calling the start or startat function.
t = timer('StartDelay', 5,'TimerFcn', ...
'disp(''Hello World!'')');Start the timer object running.
start(t)
After the start function returns, call the wait function immediately. The wait function blocks the command line until the timer object fires.
wait(t)
Delete the timer object after you are finished using it.
delete(t)
![]() | Working with Timer Object Properties | Creating and Executing Callback Functions | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |