Simple sample:
>> t=timer('TimerFcn','disp("Hello!")','Period',3,'ExecutionMode','fixedSpacing','TasksToExecute',5);
>> start(t)
要说明的是定时的时间会在某个时刻到达,但此时不一定会立刻执行TimerFcn,但它(TimerFcn的执行)会被立即加入到Matlab的执行语句队列中去排队,等到队列中前面的语句都执行完了,才被执行。这里语句的执行也会花一段时间。执行模式就针对这些3个时刻而提出:加入执行语句队列的时刻,开始执行的时刻,执行完毕的时刻
ExecutionMode 执行的模式:
'singleShot' :只能执行一次,其他模式都可以执行多次
'fixedSpacing' :上一次执行完毕的时刻到下一次被加入队列的时刻之间的间隔是指定的固定时间长度
'fixedDelay' :上一次开始执行的时刻到下一次被加入队列的时刻之间的间隔是指定的固定时间长度
'fixedRate' :两次被加入到执行语句队列的时刻之间的间隔是指定的固定时间长度
Period 指定的时间间隔: 秒为单位,通常最小值为毫秒, 默认为1(秒)
StartDelay 启动时延 : 从调用start(t1),开始到第一次把TimerFcn的执行加入到Matlab的执行语句队列中去的时延, 默认值为0(秒)
TasksToExecute TimerFcn将被执行的次数,默认为1(次)
TimerFcn 在制定事可将被执行的函数
其它参数, 详见help文档。 不对之处,欢迎批评指正!
Property Name | Property Description | Datatypes, Values, and Defaults |
AveragePeriod | The average time between TimerFcn executions since the timer started.Note: Value is NaN until timer executes two timer callbacks. | Datatype: double Default: NaN Readonly: Always |
BusyMode | Action taken when a timer has to execute TimerFcn before the completion of previous execution of TimerFcn . | Datatype: Enumerated string Values: 'drop' Default: 'drop' Readonly: Only when Running ='on' |
ErrorFcn | Function that the timer executes when an error occurs. This function executes before the StopFcn . See Creating Timer Callback Functions for more information. | Datatype: Text string, function handle, or cell array. Default: Readonly: Never |
ExecutionMode | Determines how the timer object schedules timer events. See Timer Execution Modes for more information. | Datatype: Enumerated string Values: 'singleShot' Readonly: When Running='on' |
InstantPeriod | The time between the last two executions of TimerFcn . | Datatype: double Default: NaN Readonly: Always |
Name | User-supplied name | Datatype: Text string Default: 'timer- i ' , where i is a number indicating the i th timer object created this session.Note: If you issue the clear classes command, the timer object resets i to 1.Readonly: Never |
Period | Specifies the delay, in seconds, between executions of TimerFcn . | Datatype: double Value: Any number <0.001 Default: 1.0 Readonly: When Running ='on' |
Running | Indicates whether the timer is currently executing. | Datatype: Enumerated string: Values: 'off' 'on' Default: 'off' Readonly: Always |
StartDelay | Specifies the delay, in seconds, between the start of the timer and the first execution of the function specified in TimerFcn . | Datatype: double Value: Any number <=0Default: 0 Readonly: When Running ='on' |
StartFcn | Function the timer calls when it starts. See Creating Timer Callback Functions for more information. | Datatype: Text string, function handle, or cell array Default: Readonly: Never |
StopFcn | Function the timer calls when it stops. The timer stops when:
| Datatype: Text string, function handle, or cell array. Default: Readonly: Never |
Tag | User supplied label | Datatype: Text string Default: '' (empty string) |
TasksToExecute | Specifies the number of times the timer should execute the function specified in the TimerFcn property. | Datatype: double Value: Any number <0Default: 1 Readonly: Never |
TasksExecuted | The number of times the timer has executed TimerFcn since the timer was started | Datatype: double Value: Any number <=0 Default: 0 Readonly: Always |
TimerFcn | Timer callback function. See Creating Timer Callback Functions for more information. | Datatype: Text string, function handle, or cell array. Default: Readonly: Never |
Type | Identifies the object type | Datatype: Text string Value: 'timer' Readonly: Always |
UserData | User-supplied data | Datatype: User-defined Default: [] Readonly: Never |