The Wayback Machine - https://web.archive.org/web/20111201042642/http://www.mathworks.com:80/help/techdoc/matlab_prog/f9-38093.html
Skip to Main Content Skip to Search
Product Documentation

Creating Timer Objects

Creating the Object

To use a timer in MATLAB, you must create a timer object. The timer object represents the timer in MATLAB, supporting various properties and functions that control its behavior.

To create a timer object, use the timer function. This creates a valid timer object with default values for most properties. The following shows an example of the default timer object and its summary display:

t = timer
Timer Object: timer-1

   Timer Settings
      ExecutionMode: singleShot
             Period: 1
           BusyMode: drop
            Running: off

   Callbacks
           TimerFcn: ''
           ErrorFcn: ''
           StartFcn: ''
            StopFcn: ''

MATLAB names the timer object timer-1. (See Naming the Object for more information.)

To specify the value of timer object properties after you create it, you can use the set function. This example sets the value of the TimerFcn property and the StartDelay property. For more information about timer object properties, see Working with Timer Object Properties.

set(t,'TimerFcn',@(x,y)disp('Hello World!'),'StartDelay',5)

You can also set timer object properties when you create the timer object by specifying property name and value pairs as arguments to the timer function. The following example sets the same properties at object creation time:

t = timer('TimerFcn', @(x,y)disp('Hello World!'),'StartDelay',5);

Always delete timer objects when you are done using them. See Deleting Timer Objects from Memory for more information.

Naming the Object

MATLAB assigns a name to each timer object you create. This name has the form 'timer-i', where i is a number representing the total number of timer objects created this session.

For example, the first time you call the timer function to create a timer object, MATLAB names the object timer-1. If you call the timer function again to create another timer object, MATLAB names the object timer-2.

MATLAB keeps incrementing the number associated with each timer object it creates, even if you delete the timer objects you already created. For example, if you delete the first two timer objects and create a new object, MATLAB names it timer-3, even though the other two timer objects no longer exist in memory. To reset the numeric part of timer object names to 1, execute the clear classes command.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A; sessions led by MATLAB experts.

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