How to pause timer.

조회 수: 15 (최근 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에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by