MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 690 and 691: 9 Classes and Objects end if d >= 2
- Page 692 and 693: 9 Classes and Objects % SUBSREF swi
- Page 694 and 695: 9 Classes and Objects q = polynom(q
- Page 696 and 697: 9 Classes and Objects % POLYNOM/DIF
- Page 698 and 699: 9 Classes and Objects Building on O
- Page 700 and 701: 9 Classes and Objects Multiple Inhe
- Page 702 and 703: 9 Classes and Objects Inheritance M
- Page 704 and 705: 9 Classes and Objects a.currentValu
- Page 706 and 707: 9 Classes and Objects error('Asset
- Page 708 and 709: 9 Classes and Objects case 'date' a
- Page 710 and 711: 9 Classes and Objects Note that the
- Page 712 and 713: 9 Classes and Objects For example,
- Page 714 and 715: 9 Classes and Objects The Stock sub
- Page 716 and 717: 9 Classes and Objects case '.' swit
- Page 718 and 719: 9 Classes and Objects Example — T
- Page 720 and 721: 9 Classes and Objects if isa(name,'
- Page 722 and 723: 9 Classes and Objects end if saving
- Page 724 and 725: 9 Classes and Objects Saving and Lo
- Page 726 and 727: 9 Classes and Objects • subsref
- Page 728 and 729: 9 Classes and Objects function n =
- Page 730 and 731: 9 Classes and Objects Object Preced
- Page 732 and 733: 9 Classes and Objects How MATLAB De
- Page 734 and 735: 9 Classes and Objects Private funct
- Page 736 and 737: 9 Classes and Objects 9-76
- Page 738 and 739: 10 Scheduling Program Execution wit
- Page 742 and 743: 10 Scheduling Program Execution wit
- Page 744 and 745: 10 Scheduling Program Execution wit
- Page 746 and 747: 10 Scheduling Program Execution wit
- Page 748 and 749: 10 Scheduling Program Execution wit
- Page 750 and 751: 10 Scheduling Program Execution wit
- Page 752 and 753: 10 Scheduling Program Execution wit
- Page 754 and 755: 10 Scheduling Program Execution wit
- Page 756 and 757: 10 Scheduling Program Execution wit
- Page 758 and 759: 10 Scheduling Program Execution wit
- Page 760 and 761: 10 Scheduling Program Execution wit
- Page 762 and 763: 11 Improving Performance and Memory
- Page 764 and 765: 11 Improving Performance and Memory
- Page 766 and 767: 11 Improving Performance and Memory
- Page 768 and 769: 11 Improving Performance and Memory
- Page 770 and 771: 11 Improving Performance and Memory
- Page 772 and 773: 11 Improving Performance and Memory
- Page 774 and 775: 11 Improving Performance and Memory
- Page 776 and 777: 11 Improving Performance and Memory
- Page 778 and 779: 11 Improving Performance and Memory
- Page 780 and 781: 11 Improving Performance and Memory
- Page 782 and 783: 11 Improving Performance and Memory
- Page 784 and 785: 11 Improving Performance and Memory
- Page 786 and 787: 12 Programming Tips MATLAB Path (p.
- Page 788 and 789: 12 Programming Tips numberOfLines)
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