Timer(参考matlab help):

来源:互联网 发布:淘宝特价商城 编辑:程序博客网 时间:2024/05/18 02:43
创建定时器对象:
语法:  
T = timer[创建一个默认状态的定时器对象]
T = timer('PropertyName1', PropertyValue1, 'PropertyName2', P  ropertyValue2,...)[创建一个拥有给定参数的定时器对象]
例子
t = timer('TimerFcn',@mycallback, 'Period', 10.0);
定时器对象属性:
读取一个特定定时器对象的属性值可以用get(timer)函数,设定一个特定定时器对象的属性值可以用set(timer)函数。
常用属性:
BusyMode:当定时器需要执行TimerFcn,但前一次的TimerFcn仍然在执行的时候,此属性生效。
属性值:  'drop' — Do not execute the function.(默认)
        'error' — Generate an error. Requires ErrorFcn to be set.
         'queue' — Execute function at next opportunity.
ExecutionMode:决定定时器对象如何安排定时事件,
属性值:   'singleShot'(默认值)
'fixedDelay'
'fixedRate'
'fixedSpacing'
To execute a timer callback function once, set the ExecutionMode property to 'singleShot'.
其他3种方式:
处理回调函数队列冲突问题:
在多元化处理模式下(即'fixedDelay'、'fixedRate'、'fixedSpacing'),
如果,在忙时,定时器可能需要在先前的TimerFcn运行完成之前向matlab执
列队列增加TimerFcn,在BusyMode这个属性下,你可以设置发生此异常时的应
对方法。
 
Name:用户提供的定时器名称(默认为’timer-i’)
 
Period:定时器触发周期(默认值为1s,数据类型double,最小定时时长0.001)
 
StartDelay:指定从定时器开始到第一次执行callback函数的延时时长(数据类型double,值的范围:大于0的数,默认值为0)[即若加此属性,第一次定时时间为Period + StartDelay]
 
StartFcn:定时器开启时的回调函数
 
StopFcn:定时器停止时的回调函数
定时器停止条件:1、运行stop(timer)函数
               2、定时器执行TimerFcn并完成函数内容(i.e., the value of TasksExecuted reaches the limit set by TasksToExecute.[即定时器执行次数到达设定值])
               3、发生错误
TasksToExecute:指定定时器执行次数(数据类型:double,值:大于0,默认值:Inf)
TasksExecuted:开启定时器后,执行TimerFcn的次数
TimerFcn:timer的回调函数
生成&执行回调函数:
注意:如果回调被卷入一个高占用CPU的进程(例如更新图片)回调函数处理可能会被延迟调用当你创建一个回调函数时,前两个参数一定为‘一个定时对象的句柄’和‘一个事件的结构体’(即obj, event)。这两个参数为matlab自带的,可以不需要理会它。event参数包含两个域:‘类型’和‘数据’。其中‘类型’包含用于识别引起回调函数事件类型的一个文本字符串,即('StartFcn', 'StopFcn', 'TimerFcn', or 'ErrorFcn');而‘数据’域则包含时间、时间的产生。
简单的例子:
function my_callback_fcn(obj, event, string_arg)%传入参数,前两个为默认参数,其中event.Type为回调函数类型,event.Data为回调函数数据
txt1 = ' event occurred at ';
txt2 = string_arg;
event_type = event.Type;%get type
event_time = datestr(event.Data.time);%get timer period
msg = [event_type txt1 event_time];
disp(msg)
disp(txt2)
指定回调函数的属性值:
你可以将一个回调函数指定为一个文本字符串、矩阵、或者函数句柄。
回调函数参数传递方式:
定时器运用全步骤:
1、Create a timer object.
t = timer('StartDelay', 4,'Period', 4,'TasksToExecute', 2,...'ExecutionMode','fixedRate');
2、Specify the value of the StartFcn callback. Note that the example specifies the value in a cell array because the callback function needs to access arguments passed to it.
t.StartFcn = {'my_callback_fcn', 'My start message'};
3、Specify the value of the StopFcn callback. The example specifies the callback function by its handle, rather than as a text string. Again, the value is specified in a cell array because the callback function needs to access the arguments passed to it.
t.StopFcn = { @my_callback_fcn, 'My stop message'};
4、Specify the value of the TimerFcn callback. The example specifies the MATLAB commands in a text string.
t.TimerFcn = @(x,y)disp('Hello World!');
5、Start the timer object.
start(t)
The example outputs the following.
StartFcn event occurred at 10-Mar-2004 17:16:59
My start message
Hello World!
Hello World!
StopFcn event occurred at 10-Mar-2004 17:16:59
My stop message
6、Delete the timer object after you are finished with it.
delete(t)

0 0
原创粉丝点击