matlab timer help pls

Hello,
I am new to Matlab and am seeking help regarding stopping a timer function. I apologise in advance if I use the wrong terminology.
I have both a start and stop button. I have been able to create the timer and both buttons.
Cheers

回答(1 个)

sufyan - since you are using GUIDE for your GUI, then save the timer handle to the handles structure so that it will be available in the stop button callback. For example,
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% timer function and global variable
handles.ctr = 1;
handles.hTimer = timer('TimerFcn',{@timerCallback,handles},...
'Period',.25,'ExecutionMode','fixedRate');
start(handles.hTimer);
guidata(hObject, handles);
% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject handle to stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles,'hTimer')
stop(handles.hTimer);
end
Note how the hTimer is includes as a field in the handles structure and then we save that updated structure by calling
guidata(hObject,handles);
(I did the same for your ctr. Try to avoid using globals where possible...)
In the stop button callback, we then check to see if the handles structure has a field named hTimer and, if so, stop the timer.

4 个评论

Hi, thanks for the quick reply much appreciated but there is other code dependent on this timer so I can not easily just change the code to handles. Is there anyway you know to get a stop button to work with the code I have provided?
sufyan - can you explain or show code that describes why there is other code dependent on this timer so I can not easily just change the code to handles. All you are doing is saving the handle to the timer (which you already have) to the handles structure. How does that impact the rest of the code? It isn't clear to me why it would be.
If you are intent on using your code as is, you could try using timerfindall to get a list of timer objects. If we assume that there is only the one timer (your t) then you should be able to call stop on that timer
function stop_Callback(hObject, eventdata, handles)
timerObjs = timerfindall;
if ~isempty(timerObjs)
stop(timerObjs);
end
Hi Geoff, thank you for your reply. I have added your code and it has stopped the data from running which is helpful, i know need the data to be deleted not just paused when STOP is clicked. Any ideas?
Hi sufyan - please clarify what you mean by I now need the data to be deleted not just paused when STOP is clicked. What data is to be deleted? What is paused?

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by