How to pause timer.

5 次查看(过去 30 天)
Nicholas Ng
Nicholas Ng 2014-10-16
评论: Nicholas Ng 2014-10-17
Hi guys. I'm having a function file for a GUI to start a timer count
function Start_Timer()
global myTimer ;
% Execute function Elapsed_Time
handles = guidata(gcf);
T = 0;
myTimer = timer('TimerFcn',{@Elapsed_Time, handles}, ...
'Period', 1, 'TasksToExecute', 999);
set(myTimer, 'ExecutionMode', 'fixedRate');
set(myTimer, 'UserData', T);
start(myTimer);
end
I made another GUI to pause the timer but i'm not sure how do i pause the timer.
function Pause_Timer()
How do I do it? :S

回答(1 个)

Geoff Hayes
Geoff Hayes 2014-10-17
Nicholas - rather than using a global variable for your timer, save this object to the handles structure so that you can access it from other functions (callbacks, etc.). Try the following
function Start_Timer()
handles = guidata(gcf);
T = 0;
handles.myTimer = timer('TimerFcn',{@Elapsed_Time, handles}, ...
'Period', 1, 'TasksToExecute', 999);
% update the handles structure
guidata(gcf,handles);
set(handles.myTimer, 'ExecutionMode', 'fixedRate');
set(handles.myTimer, 'UserData', T);
% start the timer
start(handles.myTimer);
Now, I'm not clear by what you mean by I made another GUI to pause..., so I will assume that you have just one GUI and have some other functionality (a button perhaps?) that when pressed will pause the timer. I think though, you will have to stop the timer and restart it, as there doesn't seem to be a pause command. So you may need to do something like
function Pause_Timer()
handles = guidata(gcf);
if strcmpi(get(handles.myTimer,'Running'),'on')
% timer is running, so stop it
stop(handles.myTimer);
else
% timer is not running, so start it
start(handles.myTimer);
end
  3 个评论
Geoff Hayes
Geoff Hayes 2014-10-17
Something similar to pause is possible by stopping and re-starting the timer, as shown above. So that may be sufficient for your purposes. It seems to maintain "state" between stops and starts, and by that I mean, the UserData keeps its previous values from before the timer was stopped.
Nicholas Ng
Nicholas Ng 2014-10-17
Ok Thanks ! I'll try doing it now :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by