前言
某位同学毕设需要时间精确同步,因此,拜托我写一个定时产生信号的程序,要学就学扎实,因此顺便就把matlab 里面timer的文档给看了一遍。
正文
一下是help timer的内容,将对此进行分析,配合例子去理解效果更好哦
MATLAB Timer Object Properties and Methods.
时间对象的属性
AveragePeriod - 平均执行周期
BusyMode - Action taken when TimerFcn executions are in progress.
当时间函数执行过程中采取的动作
ErrorFcn - Callback function executed when an error occurs.
错误发生时的回掉函数
ExecutionMode - Mode used to schedule timer events.
时间事件的调度方式
InstantPeriod - Elapsed time between the last two TimerFcn executions.
两个时间函数执行的间隔
Name - Descriptive name of the timer object.
定时器对象的名字
Period - Seconds between TimerFcn executions.
定时间执行间隔
Running - Timer object running status.
定时器运行状态
StartDelay - Delay between START and the first scheduled TimerFcn execution.
定时器函数开始和第一次调度之前的延迟。
StartFcn - Callback function executed when timer object starts.
定时器对象开始时的回调对象
StopFcn - Callback function executed after timer object stops.
定时器结束时的回调函数
Tag - Label for object.
定时器对象的标签
TasksToExecute - Number of times to execute the TimerFcn callback.
定时器函数执行的次数
TimerFcn - Callback function executed when a timer event occurs.
定时器事件发生时执行的函数
Type - Object type.
对象类型
UserData - User data for timer object.
时间对象使用者的数据。
timer methods:
定时器方法
Timer object construction:
定时器对象创建
@timer/timer - Construct timer object.
创建定时器对象
Getting and setting parameters:
获得或者设置定时器参数
get - Get value of timer object property.
得到定时器对象的属性
set - Set value of timer object property.
设置定时器对象的属性。
General:
通用方法
delete - Remove timer object from memory.
删除
display - Display method for timer objects.
展示
inspect - Open the inspector and inspect timer
观察者
object properties.
对象属性
isvalid - True for valid timer objects.
合法吗?
length - Determine length of timer object array.
定时器对象矩阵的长度
size - Determine size of timer object array.
定时器对象的尺寸
timerfind - Find visible timer objects with specified
找到定时器 找到特定的可视化定时器
property values.
对象属性
timerfindall - Find all timer objects with specified property values.
找到所有具有特定值的定时器
Execution:
执行
start - Start timer object running.
开始定时器
startat - Start timer object running at a specified time.
在特定时刻开始定时器
stop - Stop timer object running.
停止定时器运转
wait - Wait for timer object to stop running.
等待定时器停止运转
例子
1一个每隔0.1秒,输出一次y值的程序,总共输出20次
y=0.1
t=timer('Timefcn','disp(y);','Period',0.1,'ExcutionMode','fixedSpacing','TasksToExcute',10)
start(t)
解释一下,第一个选项时回调函数,输出y值,第二个选项是周期0.1,第三个选项是执行模式,停靠,第四个是执行次数,10
2输出一个
函数,即t时刻以前是0,t时刻以后是1,时间间隔依旧是0.1,输出时间为10s。
x=0;
y=1;
td=3;
t1=timer(‘Timefcn’,’disp(x);’,’Period’,0.1,’ExcutionMode’,’fixedSpacing’,’TasksToExcute’,td*10)
t2=timer(‘Timefcn’,’disp(y);timer(‘Timefcn’,’disp(y);’,’Period’,0.1,’ExcutionMode’,’fixedSpacing’,’TasksToExcute’,(10-td)*10,‘StartDelay’,td*10)
start(t1)
start(t2)
这个start的顺序不影响执行时间,这个代码本身执行是很快的。
参考:
mathwork官方文档