Matlab的多線程工具-Timer對象

來源:互聯網
上載者:User

一、Timer對象的建立

T = timerT = timer('PropertyName1', PropertyValue1, 'PropertyName2', PropertyValue2,...)

括弧中的參數可以在建立Timer是給出,也可以通過以後的set/get命令設定和讀取

二、Timer屬性參數

1.屬性設定
(1)建立的時候直接賦值
%建立一個Timer物件變數TaskTimer,並給出部分參數

TaskTimer=timer(...    'Name','Matlabsky',...    'TimerFcn',@ExecuteTask,...    'ErrorFcn',@ExecuteError,...    'Period',1,...    'ExecutionMode','fixedrate');  

(2)使用set/get設定和讀取

%建立一個預設的Timer對象TaskTimer=timerset(TaskTimer,'Name','Matlabsky','TimerFcn',@ExecutTask)get(TaskTimer,'Name')

其實set/get的使用方法不只這個,還其他形式,特別是在同時設定多個控制代碼的多個屬性時,特別方便,感興趣的網友可以直接參考Matlab的協助文檔

(3)使用結構體設定

TaskTimer=timerTaskTimer.Name='Matlabsky'TaskTimer.TimerFcn=@ExecutTask

2.常用屬性(下面是幾個比較重要的,希望大家掌握)
(1)ExecutionMode   執行的模式
就是確定TimerFcn執行循環計算的始末點
'singleShot'    :只能執行一次,故Period屬性不起作用,其他模式都可以執行多次
'fixedSpacing'  :上一次TimerFcn執行完畢時刻到下一次TimerFcn被排入佇列時刻之間的間隔
'fixedDelay'    :上一次開始執行到下一次被排入佇列之間的間隔     
'fixedRate'     :前後兩次被加入到執行語句隊列時刻之間的間隔

(2)Period  時間間隔(執行循環)
也就是每個Period執行一次TimerFcn,執行循環到底指的是哪兩個時刻之間的間隔由ExecutionMode決定

(3)StartDelay   啟動時延
從啟動Timer開始到第一次把TimerFcn的加入到Matlab的執行語句隊列中去的時延, 預設值為0s          

(4)TasksToExecute    執行次數
TimerFcn被執行的次數,預設為1次,設定多次時需要設定執行循環Period

(5)TimerFcn    Timer函數
是Timer對象的核心,Timer的多線就是通過執行這個TimerFcn來實現的,也是Timer對象的必選參數

當然還有其它屬性,但是相對來說比較次要,更詳細的說明,感興趣網友可以自己查看MATLAB協助文檔

三、回呼函數callback

1.callback的建立
Timer對象的回呼函數有TimerFcn、ErrorFcn、StartFcn和StopFcn,其中TimerFcn是必選的
Timer的所有回呼函數callback,至少有兩個輸入參數obj和event,其經典格式為

function my_callback_fcn(obj,event,p1, p2)%by dynamic%see also http://www.matlabsky.com%20092.15%%obj和event為必選輸入參數%%obj就是前面建立的Timer對象,它包含著Timer的所有參數%%event是一個結構體,包含Type和Data兩個欄位,Type儲存的是當前的Timer對象執行的時間,比如StartFcn、StopFcn等%%而Data還是一個結構體,包含time一個欄位,儲存的是執行該事件的系統時間%%所以event的使用是event.Type擷取Timer到底在執行哪個事件,event.Data.time或者執行該時間的系統時間%%p1,p2...等是其它自訂的輸入參數,根據需要在自己添加

2.回呼函數的屬性值

 回呼函數                             屬性值
function myfcn(obj,event)            set(T,'StartFcn','myfcn')或者T.StartFcn='myfcn'(下同)
function myfcn(obj,event)            set(T,'StartFcn',@myfcn)(同上)
function myfcn(obj,event,p1,p2)      set(T,'StartFcn',{'myfcn',p1,p2})或者T.StartFcn={'myfcn',p1,p2}(下同)
function myfcn(obj,event,p1,p2)      set(T,'StartFcn',{@myfcn,p1,p2})(同上)
 

3.回呼函數執行個體

function TimerFcn_callback(obj,event,var)txt=' event occurred at ';event_type = event.Type;%擷取當前事件event_time = datestr(event.Data.time);msg = [event_type txt event_time];disp('Hello it's Matlabsky Timer Object Demo')disp('Now! Begin...')disp(msg)x=2*pi*[-1:0.01:1];y=var*sin(var*x)+varplot(x,y) 

 

 四、Timer對象應用執行個體

function matlabsky_timer_demo%%by dynamic%see also http://www.matlabsky.com%2009.1.23%clc%建立Timer對象T = timer('TimerFcn',@TimerFcn, 'StartDelay',10,'StartFcn',@StartFcn);%啟動Timerstart(T);%開始迴圈while T.userdata=='f'       disp('Love Matlab Love Matlabsky——Timer Object Demo');    pause(1)end%終止Timerstop(T);   function TimerFcn(obj, event)%10s後終止程式obj.userdata='t';disp(['the BeiJing time is ' datestr(event.Data.time)])disp('10s Has Escaped, Following The Orde of TimerFcn, Now I Have To Stop This Progam!')function StartFcn(obj,event)obj.userdata='f';disp(['the BeiJing time is ' datestr(event.Data.time)])disp('The Timer Object Demo Begin Now!')

程式運行結果為

 the BeiJing time is 17-Feb-2009 13:36:05The Timer Object Demo Begin Now!Love Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object DemoLove Matlab Love Matlabsky——Timer Object Demothe BeiJing time is 17-Feb-2009 13:36:1510s Has Escaped, Following The Orde of TimerFcn, Now I Have To Stop This Progam!

這裡還一個Timer對象在【股票瀏覽器】GUI中的應用的執行個體,感興趣的可以看看http://www.matlabsky.com/thread-656-1-1.html

 

五、與Timer相關的其它函數

timer      建立Timer對象
set/get      設定/擷取Timer屬性
start      啟動Timer
startat      在指定時間啟動Timer
stop      終止Timer
disp      顯示Timer所有屬性
timerfind      找出記憶體中可見的Timer
timerfindall      找出記憶體中的所有Timer
wait      等待Timer執行完了以後在執行後面的命令列
delete      從記憶體中刪除Timer

(以上內容引自:http://blog.sina.com.cn/s/blog_6b7efa49010188te.html)

六、特別需要注意的事項

    在使用Timer對象的實際過程中,我還是碰到了一些意外情況,白費了不少功夫,記錄如下:

    1)Matlab命令列中操作Timer

        stop(timerfind) 停止當前正在執行的Timer對象

        delete(timerfind) 刪除當前的Timer對象

    2)獨立啟動並執行Matlab程式中使用Timer需注意

        如果是將.m檔案直接編譯成exe,運行時可能會出現Timer執行一次後即關閉的現象。儘管為Timer定義了Period屬性,而且ExecutionMode='fixedrate',在Matlab命令列中運行時,一切正常,可單獨運行時,只能運行一次!經過摸索發現,原因出在我設計的這個程式沒有figure視窗,導致Timer對象找不到宿主程式,所以退出了,後來不得不將程式改成GUI,重新編譯成exe後,一切正常!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.