I. Create a timer object
T = timerT = timer('PropertyName1', PropertyValue1, 'PropertyName2', PropertyValue2,...)
Parameters in brackets can be provided when timer is created, or you can set and read through the set/get command later.
Ii. Timer attribute Parameters
1. attribute settings
(1) assign values directly during creation
% Create a timer object variable tasktimer and provide some parameters
TaskTimer=timer(... 'Name','Matlabsky',... 'TimerFcn',@ExecuteTask,... 'ErrorFcn',@ExecuteError,... 'Period',1,... 'ExecutionMode','fixedrate');
(2) Set and read with set/get
% Create a default timer object tasktimer = timerset (tasktimer, 'name', 'text', 'timerfcn ', @ executtask) Get (tasktimer, 'name ')
In fact, the use of set/get is not only this, but also other forms, especially when setting multiple attributes of multiple handles at the same time, it is particularly convenient, interested users can directly refer to the help documentation of MATLAB
(3) use struct settings
TaskTimer=timerTaskTimer.Name='Matlabsky'TaskTimer.TimerFcn=@ExecutTask
2. Common attributes (below are several important attributes that you hope to master)
(1) executionmode Execution Mode
Is to determine the start and end points of timerfcn execution cycle calculation
'Singleshot ': it can only be executed once, so the period attribute does not work. Other modes can be executed multiple times.
'Fixedspacing': interval between the last time timerfcn was executed and the next time timerfcn was added to the queue
'Fixeddelay': interval between the last execution and the next addition to the queue
'Fixedrate': interval between the first and second times added to the execution statement queue
(2) period (execution cycle)
That is, timerfcn is executed once for each period. The interval between the two time periods is determined by executionmode.
(3) startdelay startup latency
Latency from starting timer to adding timerfcn to the execution statement queue of MATLAB for the first time. The default value is 0 s.
(4) taskstoexecute execution times
The number of times timerfcn is executed. The default value is 1. If you want to set multiple times, you need to set the execution period (period ).
(5) timerfcn timer function
It is the core of the timer object. The timer multiline is implemented by executing the timerfcn, which is also a required parameter of the timer object.
Of course, there are other attributes, but they are relatively secondary. For more details, you can view the help documentation of Matlab.
Iii. Callback Function callback
1. Establish callback
Timer object callback functions include timerfcn, errorfcn, startfcn, and stopfcn. timerfcn is required.
The callback function of timer has at least two input parameters, OBJ and event. The classic format is
Function my_callback_fcn (OBJ, event, P1, P2) % by dynamic % See also http://www.matlabsky.com % 20092.15% % OBJ and event are mandatory input parameters % obj is the timer object created earlier, it contains all the timer parameters % event is a struct, including the type and data fields, the type stores the current timer object execution time, for example, startfcn, stopfcn, etc. %, and data is a struct, containing a time field, saving the system time of executing the event %, so the use of event is event. type to obtain the event that timer is executing, event. data. time or the system time % P1, p2... are other custom input parameters. Add them as needed.
2. attribute values of the callback function
Callback Function Property Value
Function myfcn (OBJ, event) Set (T, 'startfcn', 'myfcn') or T. startfcn = 'myfcn' (the same below)
Function myfcn (OBJ, event) Set (T, 'startfcn', @ myfcn) (same as above)
Function myfcn (OBJ, event, P1, P2) Set (T, 'startfcn', {'myfcn', P1, P2}) or T. startfcn = {'myfcn', P1, P2} (same below)
Function myfcn (OBJ, event, P1, P2) Set (T, 'startfcn', {@ myfcn, P1, P2}) (same as above)
3. Callback Function instance
Function timerfcn_callback (OBJ, event, VAR) TXT = 'event occurred at '; event_type = event. type; % get the current event event_time = datestr (event. data. time); MSG = [event_type TXT event_time]; disp ('Hello It's MATLAB sky timer object demo') disp ('now! Begin... ') disp (MSG) x = 2 * pI * [-1:0. 01:1]; y = Var * sin (Var * x) + varplot (x, y)
4. Timer object application example
Function maid http://www.matlabsky.com % 2009.1.23% CLC % create timer object T = timer ('timerfcn', @ timerfcn, 'startdelay', 10, 'startfcn ', @ startfcn); % start timerstart (t); % start loop while T. userdata = 'F' disp ('Love MATLAB love phpsky -- timer object demo'); pause (1) end % terminate timerstop (t); function timerfcn (OBJ, event) terminate the program OBJ after % 10s. userdata = 'T'; disp ([the Beijing time is 'datestr (event. data. ti Me)]) disp ('10s has escaped, following the orde of timerfcn, now I have to stop this progam! ') Function startfcn (OBJ, event) obj. userdata = 'F'; disp ([the Beijing time is 'datestr (event. data. time)]) disp ('the timer object demo begin now! ')
The program running result is
the BeiJing time is 17-Feb-2009 13:36:05The Timer Object Demo Begin Now!Love Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object Demothe BeiJing time is 17-Feb-2009 13:36:1510s Has Escaped, Following The Orde of TimerFcn, Now I Have To Stop This Progam!
Here is also an instance of the timer object in the [Stock browser] GUI application, interested can look at the http://www.matlabsky.com/thread-656-1-1.html
5. Other timer-related functions
Create a timer object
Set/get settings/get timer attributes
Start start Timer
Startat start timer at the specified time
Stop to terminate Timer
Disp: Display All timer attributes
Timerfind find the visible timer in memory
Timerfindall: Find all the timers in the memory.
Wait waits for timer to run the command line after it is executed
Delete: delete timer from memory
(The above content from: http://blog.sina.com.cn/s/blog_6b7efa49010188te.html)
Vi. Special precautions
In the actual process of using the timer object, I still encountered some unexpected situations. It took a lot of effort to record them as follows:
1) timer operations in the MATLAB command line
Stop (timerfind) stops the currently executed timer object
Delete (timerfind) deletes the current timer object
2) Note that timer is used in independent MATLAB programs.
If the. M file is directly compiled into an EXE, the timer may be shut down after it is executed once. Although the period attribute is defined for timer and executionmode = 'fixedrate', when running in the MATLAB command line, everything is normal and can be run only once separately! After exploration, I found that the reason is that the program I designed does not have a figure window. As a result, the timer object could not find the host program, so I quit. Then I had to change the program to the GUI and re-compile it into the exe, everything is normal!