Programming and Data Types | ![]() ![]() |
Using MATLAB Timers
MATLAB includes a timer object that you can use to schedule the execution of MATLAB commands. To use a timer, perform these steps:
timer
function. See Creating and Deleting Timer Objects for more information.
start
or startat
function. See Starting and Stopping Timers for more information.
Note
Because the timer works within the MATLAB single-threaded execution environment, there can be a variance between the specified execution time and the actual execution of timer callback functions. The length of this time lag is dependent on what other processing MATLAB is performing. To force the execution of the callback functions in the event queue, include a call to the drawnow function in your code. The drawnow function flushes the event queue. For more information about callback processing, see The Event Queue in the MATLAB GUI programming documentation.
|
For example, this code sets up a timer that executes a MATLAB command string after 10 seconds elapse. The example creates a timer object, specifying the values of two timer object properties, TimerFcn
and StartDelay
. TimerFcn
specifies the timer callback function. This is the MATLAB command string or M-file that you want to execute. StartDelay
specifies how much time elapses before the timer executes the callback function.
After creating the timer object, the example uses the start
function to start the timer. (The additional commands in this example are included to illustrate the timer but are not required for timer operation.)
t = timer('TimerFcn', 'stat=false; disp(''Timer!'')',... 'StartDelay',10); start(t) stat=1; while(true) disp('.') pause(1) end
When you execute this code, it produces this output:
![]() | Shell Escape Functions | Creating and Deleting Timer Objects | ![]() |