Stopping a Timer via another function
15 次查看(过去 30 天)
显示 更早的评论
Hello.
Is there a way to stop a timer from another function whilst it is running. Im not sure what to pass to the other function:
This is my timer and wait part
T = timer('TimerFcn',@(~,~)disp('Fired.'),'StartDelay',10);
start(T)
wait(T)
delete(T)
I want to have a stop button which will allow me to stop if the wait hasn't finished, but not sure how to pass the timer object to that function to then apply
stop(T)
0 个评论
采纳的回答
Geoff Hayes
2015-3-14
Jason - since you have a stop button, then you must be using a GUI which has (perhaps?) been created with GUIDE. If that is the case, then just save T to the handles structure so that when the stop button is pressed, it can stop the timer.
Suppose we have a simple GUI with two buttons - start and stop. The callback for the former starts the timer as
function start_Callback(hObject, eventdata, handles)
handles.myTimer = timer('Name','MyTimer', ...
'Period',1.0, ...
'StartDelay',1, ...
'TasksToExecute',30, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',@(~,~)disp('Fired.'));
guidata(hObject,handles);
start(handles.myTimer);
wait(handles.myTimer);
fprintf('timer has stopped');
delete(handles.myTimer);
In the above, we create a timer that fires every one second for 30 times (so 30 seconds). If we launch the GUI and press the start button, we see Fired displayed 30 times in the console before the timer has stopped message is written.
Now suppose our stop callback is
function stop_Callback(hObject, eventdata, handles)
if isfield(handles,'myTimer')
stop(handles.myTimer);
end
Again, we launch the GUI and press the start button and observe the messages written to the console. But now we press the stop button before the 30 seconds is up and we observe that the timer has stopped message appears within a second.
Try the above and see what happens!
3 个评论
Geoff Hayes
2015-3-17
Jason - have you omitted some code from the above sample because I don't think that the above is valid especially with the
<snip!>
TasksToExecute',t, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',@(~,~)disp('Fired.')), ...;
waitbar(step / t), ...
step=step+1);
Note the semicolon at the end of the third line. What should go here instead?
If you want your timer to control the updates to a progress/wait bar, then you may have to supply your own callback to the timer. Something like
function Timer1(hObject,handles,t)
handles.t = t;
handles.hwb = waitbar(0,'please Wait');
handles.step = 0;
handles.myTimer = timer('Name','MyTimer', ...
'Period',1.0, ...
'StartDelay',1, ...
'TasksToExecute',t, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn', {@myTimerCallback,hObject});
guidata(hObject,handles);
start(handles.myTimer);
wait(handles.myTimer);
fprintf('timer has stopped');
delete(handles.myTimer);
%Close waitbar
close(handles.hwb)
where your callback would look like
function myTimerCallback(~,~,hObject)
handles = guidata(hObject);
handles.step = handles.step + 1;
waitbar(handles.step/handles.t, handles.hwb);
guidata(hObject,handles);
The above code is very similar to what you had with the logic to update the waitbar in the timer callback function.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!