TIMER still runs after deleting the variable

currentTimeSecs = rem(now,1)*24*60*60;
fireTimerAtSecs = 15*60*60;
if currentTimeSecs < fireTimerAtSecs
% timer will fire today
timerDelaySecs = fireTimerAtSecs - currentTimeSecs;
else
% timer will fire tomorrow
timerDelaySecs = (24*60*60 - currentTimeSecs) + fireTimerAtSecs;
end
T = timer('Period',120, ...
'ExecutionMode','fixedRate', ...
'StartDelay', timerDelaySecs, ...
'TimerFcn',@(src,evt)disp('hi'));
start(T);
When I run the above code and then delete the variable T then the script still continues running and executing the function disp(hi). How can I stop that? how can i stop the timer?

 채택된 답변

Image Analyst
Image Analyst 2015년 2월 1일

2 개 추천

Check out this snippet of code that I wrote to stop all timers. Works even if you have lost the handle to some timer you started up:
%--------------------------------------------------------------------------------------------------------------------------
function StopTimer(handles)
try
fprintf('Entering StopTimer...\n');
listOfTimers = timerfindall % List all timers, just for info.
% Get handle to the one timer that we should have.
if isempty(listOfTimers)
% Exit if there is no timer to turn off.
fprintf('There are no timers to turn off. Leaving StopTimer().\n');
return;
end
handleToTimer = getappdata(handles.figMainWindow, 'timerObj');
% Stop that timer.
stop(handleToTimer);
% Delete all timers from memory.
listOfTimers = timerfindall
if ~isempty(listOfTimers)
delete(listOfTimers(:));
end
fprintf('Left StopTimer and turned off all timers.\n');
catch ME
errorMessage = sprintf('Error in StopTimer().\nThe error reported by MATLAB is:\n\n%s', ME.message);
fprintf('%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from StopTimer

댓글 수: 3

AA
AA 2015년 2월 14일
it gives me the error that it cant find the handles variable. what is handles?
AA
AA 2015년 2월 14일
delete(timerfind)
Image Analyst
Image Analyst 2015년 2월 14일
편집: Image Analyst 2015년 2월 14일
This was a snippet from a GUI I had built with GUIDE. If you're not using a GUIDE GUI, you won't have handles. Just use this code:
% Delete all timers from memory.
listOfTimers = timerfindall
if ~isempty(listOfTimers)
delete(listOfTimers(:));
end
I'm not sure if you have to stop them first - I think just deleting the timer object will automatically stop it. If it doesn't then put it in a loop.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2015년 2월 1일

1 개 추천

AA - to stop the timer T, call
stop(T);

카테고리

도움말 센터File Exchange에서 Programming Utilities에 대해 자세히 알아보기

질문:

AA
2015년 2월 1일

편집:

2015년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by