How to pause timer.
13 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!