加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[转载]MATLAB 定时器

(2013-08-26 15:48:56)
标签:

转载

原文地址:MATLAB 定时器作者:王双木
MATLAB中的定时器timer可以定时触发,周期性地执行指定的函数,而不会影响整个系统对用户其它操作的响应。

建立一个定时器timer对象

T = timer; % constructs a timer object with default attributes
T = timer('PropertyName1', PropertyValue1, 'PropertyName2', PropertyValue2,...); % constructs a timer object in which the given property name/value pairs are set on the object. 

Timer对象支持的全部属性和合法值如下表所示,可以通过get(timer)函数查看timer的相关属性,也可以使用set(timer)设置timer的各类属性:

 

Property Name

Property Description

Data Types, Values, Defaults, Access

AveragePeriod

Average time between TimerFcn executions since the timer started.

Note: Value is NaN until timer executes two timer callbacks.

Data type double
Default NaN
Read only Always
BusyMode

Action taken when a timer has to execute TimerFcn before the completion of previous execution of 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. See Creating 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. See Timer 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 ith timer object created this session. To reset i to 1, execute the clear classes command.
Read only Never
ObjectVisibility

Provides a way for application developers to prevent end-user access to the timer objects created by their application. The timerfind function does not return an object whose ObjectVisibility property 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 executing TimerFcn, i.e., the value of TasksExecuted reaches the limit set by TasksToExecute.

  • An error occurs
    (The ErrorFcn is called first, followed by the StopFcn.)

See Creating Callback Functions for more information.

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 the timer should execute the function specified in the TimerFcn property.

Data type double
Values Any number > 0
Default 1
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. See Creating Callback Functions for more information.

Data type Text string, function handle, or cell array
Default None
Read only Never
Type

Identifies the object type.

Data type Text string
Values 'timer'
Read only Always
UserData

User-supplied data.

Data type User-defined
Default []
Read only Never

例1,设置一个10秒间隔的定时器,时间到则回调函数onTimer:

T = timer('Period',10.0,'ExecutionMode','FixedRate', 'TimerFcn',{@onTimer,arg});

Period 触发周期设置为10s,ExecutionMode是执行的方式,可以有三种选择,详见MATLAB Help, TimerFcn指定触发时所执行函数的句柄,在这里我们建立一个函数onTimer,arg 作为参数传递到 onTimer 中去。

注意onTimer的定义:
function onTimer (obj,events,arg)
前两个参数是必不可少的,最后的arg才是用户传递的数据。在这个函数中你基本上可以无视前两个参数。

在MATLAB Help的index中输入timer,可以察看详细的说明。 

0

  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有