matlab中的timer模块,[转载]Matlab中Timer的使用

Matlab中Timer的使用

鉴于Matlab中缺乏多线程机制,使用Timer无疑是一个很重要的工具,Matlab中Timer是一个Java对象。

(1)Timer 的定义

t=timer();

设置属性:

eg. set(t,'Name','your_timer_name');

当然可以一次性设置完成:

例如:

TaskTimer=timer(...

'Name','FebirdTimer',...

'TimerFcn',@ExecuteTask,...

'ErrorFcn',@ExecuteError,...

'Period',1,...

'ExecutionMode','fixedrate');

这里TimerFcn为Timer执行的函数,后面的‘@ExcuteTask’就是你定义的函数名

同样ErrorFcn也是一样。

Period 为执行周期,ExecutionMode为执行模式,fixedrate为固定频率。当然前面所说的都是在这个前提之上。

(2)关于TimerFcn的定义

当以TimerFcn的定义默认必须有两个参数

function ExcuteTask(obj,eventdata)

% TODO

end

其中obj为执行该函数所对应的timer对象,eventdata为事件数据,一般里面为具体时间。

当需要在ExcuteTask中传入参数的时候,那么Timer可以这样定义:

那么这时函数定义应该为:

function ExcuteTask(obj,eventdata,var1)

% TODO

end

其他函数的定义也类似。

(3)关于UserData

UserData 在Timer比较有用,因为当时用上面的方法传递参数是,Matlab只会在第一次传入参数。

所以我们可以在UserData这个域中保存我们的数据。

例如:

t=[0];

lh=plot(t,sin(t),'-');

t=timer(...

'Name','MyTimer',...

'TimerFcn',@ExecuteTask,...

'ErrorFcn',@ExecuteError,...

'Period',1,'TasksToExecute',100,...

'ExecutionMode','fixedrate');

ud=struct('linehandle',lh,'count',0);

set(t,'UserData',ud);

start(t);

function ExecuteTask(obj,eventdata,UserData) ;

ud=obj.UserData;

l=ud.linehandle;

c=ud.count;

t=get(l,'XData');

y=get(l,'YData');

t=[t c];

y=[y sin(0.1*c)];

set(ud.linehandle,'XData',t,'YData',y);

drawnow; %一般放置在set命令后,用于重构刷新图形。

ud.count=ud.count+1;

set(obj,'UserData',ud);

end

以上给出了一个使用Timer画图的方法。

(4)关于Timer的函数

1.start();

2.stop();

3.timerfind();

eg.删除所有的timer

ts=timerfind;

if length(ts)>0

stop(ts);

delete(ts);

end

通过Name查找特定的Timer:

t=timerfind('Name','FebirdTimer');

例如:

% --- Executes on button press in pushbutton1.

function pushbutton1_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton1 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

tb= timer('Name','ButtonTimer','StartDelay', 4,'Period', 4,'TasksToExecute', 2,...

'ExecutionMode','fixedRate');

tb.StartFcn = {'my_callback_fcn', 'My start message'};

tb.StopFcn = { @my_callback_fcn, 'My stop message'};

tb.TimerFcn = @(x,y)disp('Hello World!');

start(tb);

% --- Executes on button press in pushbutton3.

function pushbutton3_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton3 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

tb=timerfind('Name','ButtonTimer');

if length(tb)>0

stop(tb);

delete(tb);

end

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)

end

以上给出了通过GUI button 按钮来控制timer开始和终止的方法。

(整理:程翔宇chanceller@163.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值