MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

10 Scheduling Program Execution with Timers Example: Displaying a Message The following example sets up a timer object 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 when the timer fires. In the example, the timer callback function sets the value of the MATLAB workspace variable stat and executes the MATLAB disp command. The StartDelay property specifies how much time elapses before the timer fires. After creating the timer object, the example uses the start function to start the timer object. (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=true; while(stat==true) disp('.') pause(1) end When you execute this code, it produces this output: . . . . . . . . . Timer! delete(t) % Always delete timer objects after using them. 10-4

Creating Timer Objects Creating Timer Objects TouseatimerinMATLAB,youmustcreateatimerobject.Thetimerobject 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 “Timer Object Naming” on page 10-6 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” on page 10-7. set(t,'TimerFcn','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', 'disp(''Hello World!'')','StartDelay',5); 10-5

10 Scheduling Program Execution with Timers<br />

Example: Displaying a Message<br />

The following example sets up a timer object that executes a <strong>MATLAB</strong><br />

command string after 10 seconds elapse. The example creates a timer<br />

object, specifying the values of two timer object properties, TimerFcn and<br />

StartDelay. TimerFcn specifies the timer callback function. This is the<br />

<strong>MATLAB</strong> command string or M-file that you want to execute when the<br />

timer fires. In the example, the timer callback function sets the value of<br />

the <strong>MATLAB</strong> workspace variable stat and executes the <strong>MATLAB</strong> disp<br />

command. The StartDelay property specifies how much time elapses before<br />

the timer fires.<br />

After creating the timer object, the example uses the start function to start<br />

the timer object. (The additional commands in this example are included to<br />

illustrate the timer but are not required for timer operation.)<br />

t = timer('TimerFcn', 'stat=false; disp(''Timer!'')',...<br />

'StartDelay',10);<br />

start(t)<br />

stat=true;<br />

while(stat==true)<br />

disp('.')<br />

pause(1)<br />

end<br />

When you execute this code, it produces this output:<br />

.<br />

.<br />

.<br />

.<br />

.<br />

.<br />

.<br />

.<br />

.<br />

Timer!<br />

delete(t) % Always delete timer objects after using them.<br />

10-4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!