Programming and Data Types | ![]() ![]() |
Creating and Deleting 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. You can create a timer object with default attributes and then use the set
function to specify the values of its properties, as in this example:
You can also set timer object properties when you create the timer object by specifying property name and property value pairs as arguments to the timer
function:
Deleting Timer Objects from Memory
When you are finished with a timer, delete it from memory using the delete
function:
When you delete a timer object, workspace variables that referenced the object remain. Deleted timer objects are invalid and cannot be reused. Use the clear
command to remove workspace variables that reference deleted timer objects. To test if a timer object has been deleted, use the isvalid
function. The isvalid
function returns false
for deleted timer objects:
To remove all timer objects from memory, enter
See Finding All Timer Objects Currently in Memory for information about the timerfind
function.
Finding All Timer Objects Currently in Memory
To find all the timers that are currently in memory, use the timerfind
function. This function returns an array of timer objects. If you leave off the semicolon, and there are multiple timer objects in the array, timerfind
displays summary information in a table:
t = timer; t2 = timer; t3 = timer; t_array = timerfind Timer Object Array Index: ExecutionMode: Period: TimerFcn: Name: 1 singleShot 1 '' timer-3 2 singleShot 1 '' timer-4 3 singleShot 1 '' timer-5
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. In this example, the list of timer object names start with timer-3
. This indicates that two other timer objects were previously created and then deleted in this MATLAB session. If you issue the clear classes
command, MATLAB resets i
to 1.
![]() | Using MATLAB Timers | Timer Object Properties | ![]() |