How to pause timer.

18 views (last 30 days)
Nicholas Ng
Nicholas Ng on 16 Oct 2014
Commented: Nicholas Ng on 17 Oct 2014
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

Answers (1)

Geoff Hayes
Geoff Hayes on 17 Oct 2014
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 Comments
Geoff Hayes
Geoff Hayes on 17 Oct 2014
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 on 17 Oct 2014
Ok Thanks ! I'll try doing it now :)

Sign in to comment.

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!