How to pause timer.

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 个)

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 个评论

Ahh yes. Not GUI sorry. Button haha. Oh ... I already have a button that restart the timer. I'm doing an assignment where I have to build a minesweeper game. But I'm asked to improvise or add on features... So the pause feature is not possible?? :/
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.
Ok Thanks ! I'll try doing it now :)

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Environment and Settings 的更多信息

标签

提问:

2014-10-16

评论:

2014-10-17

Community Treasure Hunt

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

Start Hunting!

Translated by