Starting and Stopping Timers

Starting a Timer

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.

  1. Create a timer object, specifying values for timer object properties.

    t = timer('TimerFcn','disp(''Hello World!'')','StartDelay', 5);
  2. Start the timer object.

    start(t)
  3. Delete the timer object after you are finished using it.

    delete(t);

Starting a Timer at a Specified Time

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);

Stopping Timer Objects

Once started, the timer object stops running if one of the following conditions apply:

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:

  1. Create a timer object.

    t = timer('TimerFcn','disp(''Hello World!'')', ...
              'StartDelay', 100);
  2. Start it running.

    start(t)
  3. Check the state of the timer object after starting it.

    get(t,'Running')
    
    ans =
    
    on
  4. 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
  5. Delete the timer object when you are finished using it.

    delete(t)

Blocking the MATLAB Command Line

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.

  1. Create a timer object.

    t = timer('StartDelay', 5,'TimerFcn', ...
              'disp(''Hello World!'')');
  2. Start the timer object running.

    start(t)

  3. After the start function returns, call the wait function immediately. The wait function blocks the command line until the timer object fires.

    wait(t)
  4. Delete the timer object after you are finished using it.

    delete(t)

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS