Timer 计时器
MATLAB的计时器主要用于完成动画任务及定时任务。
Construct timer object 构造一个计时器对象
Syntax
T = timer
T = timer('PropertyName1', PropertyValue1, 'PropertyName2', PropertyValue2,...)
Discription
T = timer constructs a timer object with default attributes.
利用默认属性构造一个计时器对象
T = timer('PropertyName1', PropertyValue1, 'PropertyName2', PropertyValue2,...) constructs atimer object in which the given property name/value pairs are set on the object.
利用给定的属性名称/属性值对来构造一个计时器对象
See Timer Object Properties for a list of all the properties supported by the timer object.
可以通过计时器属性列表来了解所有计时器对象支持的属性
Note that the property name/property value pairs can be in any format supported by the setfunction, i.e., property/value string pairs, structures, and property/value cell array pairs.
注意属性名称/属性值对儿可以利用任何的set函数支持的形式设置,例如属性名称/属性值字符串对,结构体和属性名称/属性值原包数组对儿。
Examples
下面是无开始延迟,周期为1s,执行任务无限个,执行模式为固定延时模式,回调函数为显示“hello world”字符串的计时器设置。Cmd模式直接输入即可:
T=time('StartDelay',0,'Period',1,'TasksToExecute',inf,'ExecutionMode','fixedDelay','TimerFcn','disp(''Hello World!'')');
初始化完成后,调用
start(t);
在CMD Window中显示(每秒输出一行):
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
调用stop(t),则计时器停止。
Timer Object Properties
The timer object supports the following properties that control its attributes. The table includes information about the data type of each property and its default value.
To view the value of the properties of a particular timer object, use the get(timer) function. To set the value of the properties of a timer object, use the set(timer) function.
Property Name
Property Description
Data Types, Values, Defaults, Access
AveragePeriod
Average time between TimerFcnexecutions since the timer started.
Note: Value is NaN until timerexecutes two timer callbacks.
计时器开始后的TimerFcn平均执行时间
Data type
double
Default
NaN
Read only
Always
BusyMode
Action taken when a timer has to execute TimerFcn before the completion of previous execution ofTimerFcn. 当计时器执行TimerFcn时前面的执行未完成时采取的措施
'drop' — Do not execute the function.
'error' — Generate an error. Requires ErrorFcn to be set.
'queue' — Execute function at next opportunity.
Data type
Enumerated string
Values
'drop'
'error'
'queue'
Default
'drop'
Read only
While Running = 'on'
ErrorFcn
Function that the timer executes when an error occurs. This function executes before the StopFcn. SeeCreating Callback Functions for more information.
Data type
Text string, function handle, or cell array
Default
None
Read only
Never
ExecutionMode
Determines how the timer object schedules timer events. SeeTimer Object Execution Modes for more information.
决定计时器对象的计时事件的方式。
Data type
Enumerated string
Values
'singleShot'
'fixedDelay'
'fixedRate'
'fixedSpacing'
Default
'singleShot'
Read only
While Running = 'on'
InstantPeriod
The time between the last two executions of TimerFcn.
最后两次执行时间间隔
Data type
double
Default
NaN
Read only
Always
Name
User-supplied name.
用户提供的名称
Data type
Text string
Default
'timer-i', where i is a number indicating the ithtimer object created this session. To reset i to 1, execute the clear classescommand.
Read only
Never
ObjectVisibility
Provides a way for application developers to prevent end-user access to the timer objects created by their application. Thetimerfind function does not return an object whose ObjectVisibilityproperty is set to 'off'. Objects that are not visible are still valid. If you have access to the object (for example, from within the M-file that created it), you can set its properties.
Data type
Enumerated string
Values
'off'
'on'
Default
'on'
Read only
Never
Period
Specifies the delay, in seconds, between executions of TimerFcn.
指定两次执行回调的延迟,以秒为单位
Data type
double
Value
Any number >= 0.001
Default
1.0
Read only
While Running = 'on'
Running
Indicates whether the timer is currently executing.
标志计时器是否在工作
Data type
Enumerated string
Values
'off'
'on'
Default
'off'
Read only
Always
StartDelay
Specifies the delay, in seconds, between the start of the timer and the first execution of the function specified in TimerFcn.
Data type
double
Values
Any number >= 0
Default
0
Read only
While Running ='on'
StartFcn
Function the timer calls when it starts. See Creating Callback Functions for more information.
Data type
Text string, function handle, or cell array
Default
None
Read only
Never
StopFcn
Function the timer calls when it stops. The timer stops when
You call the timer stop function
The timer finishes executingTimerFcn, i.e., the value ofTasksExecuted reaches the limit set by TasksToExecute.
An error occurs
(The ErrorFcn is called first, followed by the StopFcn.)
Date type
Text string, function handle, or cell array
Default
None
Read only
Never
Tag
User supplied label.
Data type
Text string
Default
Empty string ('')
Read only
Never
TasksToExecute
Specifies the number of times thetimer should execute the function specified in the TimerFcn property.
指定回调函数的执行次数
Data type
double
Values
Any number > 0
Default
Inf
Read only
Never
TasksExecuted
The number of times the timer has called TimerFcn since the timer was started.
计时器开启后的执行次数
Data type
double
Values
Any number >= 0
Default
0
Read only
Always
TimerFcn
Timer callback function. SeeCreating Callback Functions for more information.
Data type
Text string, function handle, or cell array
Default
None
Read only
Never