Programming Previous page   Next Page

Creating Timer Objects

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:

MATLAB names the timer object timer-1. (See Timer Object Naming 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.

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:

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

Timer Object Naming

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.


Previous page  Example: Displaying a Message Working with Timer Object Properties Next page

© 1994-2005 The MathWorks, Inc.