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

Matlab中Timer的使用

(2013-03-26 20:55:07)
标签:

matlab

timer

定时运行

定时器

axes

MatlabTimer的使用

 

鉴于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 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 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开始和终止的方法。

 

本总结意在解决如下问题:利用定时器回调函数或串口回调函数在GUI指定的axes绘图时,曲线图片总是不显示在指定的坐标轴上,总是会弹出新的窗口显示。
       利用guide来创建GUI程序时,可能大家对figure的HandleVisibility属性以及axes的NextPlot属性关注的不多。figure的HandleVisibility属性有三个属性值:onoffcallback。前两个的含义顾名思义,其作用可以看帮助文件,在此不详述。最后一个callback属性值,它是确保该figure的句柄(handle)对figure内的控件的回调函数以及回调函数内定义的函数来说是‘可见’的,而对于其它例如通过命令行来访问的用户来说是‘不可见’的,这样可以确保该figure不被无意中删除或修改其中的内容。
       可见,figure的HandleVisibility属性的callback属性值带有明显的保护figure的作用,是有益的。但是这种保护机制对于使用定时器或串口自定义回调函数来绘制曲线的用户来说,就会带来麻烦。
       因为定时器或串口对象不是figure所包含的对象,所以其回调函数在调用figure内的axes来绘图时,就会发现找不到该figure的handle,以为该figure对象不存在,于是就会新建figure窗口,在其中创建axes来显示的曲线。

    根据以上分析,如果用户在利用定时器或串口回调函数绘制图形时出现上述问题,解决办法如下:
    1、fugure‘HandleVisibily’属性值默认为‘callback’,把它改为‘on’
    2、axes的‘NextPlot’的属性值默认为‘replace’,改为‘add’
    3、在绘图语句的前面使用语句axes(handles.axes1);%'axes1'为axes的'tag'

    
   先前有会员在编程时遇到同样的问题,根据以上方法,问题顺利解决。
   希望有遇到类似问题的会员可以参考该帖子。:-)

  以上是个人经验,如有不足,欢迎指正修改,多谢。

 

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

 

0

阅读 收藏 喜欢 打印举报/Report
  

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

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

新浪公司 版权所有