Delete a timer object in it's callback
이전 댓글 표시
I have short timer callbacks to change a color from a button press or other action back to it's original color. This gives a nice flash of color when that action happens. Since the names given to a timer when it is declared don't really exist e.g."timer-1" and you don't get to know which one it is anyway, I tagged them like this:
Example: Set color then start timer
app.LickRight.Color='green';
tR = timer;
tR.Tag = "tR"; % Tag it with it's own name
tR.StartDelay = 0.1;
tR.TimerFcn = @(~,~)app.RightBlack;
start(tR);
The callback looks like this:
function RightBlack(app,~)
app.LickRight.Color='black';
stop(timerfind('Tag','tR'));
delete(timerfind('Tag','tR'));
end
break at the start of the callback after color change:
1042 stop(timerfind('Tag','tR'));
K>> timerfind('Tag','tR')
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: on
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ''
StartFcn: ''
StopFcn: ''
First question: Since it's a singleShot and I timed out into the callback why is it still Running On
OK, I can handle it with
stop(timerfind('Tag','tR'));
Step into that and check
K>> timerfind('Tag','tR')
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ''
StartFcn: ''
StopFcn: ''
OK, that worked, now it's off at least. Next line executed:
delete(timerfind('Tag','tR'));
Check again for this one:
K>> timerfind('Tag','tR')
ans =
Empty timer object: 0-by-0
That's interesting, not sure what it means. Let's check all timer objects
K>> timerfind
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ''
StartFcn: ''
StopFcn: ''
Wait what!? Is it there or not:
Let's delete everything for now (Can't do this in the real program, multiple timers could be running at once)
K>> delete(timerfind)
K>> timerfind
ans =
[]
Yeah, that's what I'm looking for. Why didn't I get it the first time?
Here's what's weird: When I try it again it does delete and I don't get what's above. How did that happen the first time? (retorical question)
Is this clear simple way to find a timer so you can delete it in the callback? Is it OK to put lessons learned here inMatlabCentral for others to find?
I'm asking any way because the HELP for this kind of thing is archaic (GUIDE) and a very long solution from IDK how long ago whith all kinds of handle manipulation. Let's show how timer cleanup can be done easily, because if you don't delete them after they run they pile up eating memory, even outlasting stopping and rerunning the program, because they are separate process threads.
Oh, one more thing. Good practice when using timers in an App Designed program to do this when app closes in the built in function?
% Close request function: MouseOdor
function MouseOdorCloseRequest(app, event)
fclose('all'); % closes all open files. This probably happens anyway?
% Clear all timers
stop(timerfindall);
delete(timerfindall);
delete(app);
end
Anyone else notice that you can't add text after a code section unless you leave some blank lines below where you are typing?
댓글 수: 2
Les Beckham
2024년 10월 3일
I can't help with your timer issues, but I can help with your comment/question about adding text after a code section in Answers. When you are done typing in a code section, either press Alt-Enter or click the far left icon in the toolbar
Walter Roberson
2024년 10월 3일
delete(timerfind('Tag','tR'));
Why are you not permitting the default parameters to pass into the timer callback? The first of the default parameters is a reference to the timer that is being referred to, which would save you a lot of timer finding
채택된 답변
추가 답변 (1개)
Bruno Luong
2024년 10월 3일
I don't know, but I have programmed the same toys, my worlflow is like this
close all;
fig = figure;
ax = axes('Parent', fig);
text(ax, 0.5, 0.5, 'Click with your mouse', 'HorizontalAlignment','center')
set(fig, 'WindowButtonDownFcn', @(varargin) Blinking(ax))
%%
function Blinking(ax)
% Callback when the user use mouse right-click on the figure
% ZOOM and PAN must turned off
mouse = get(ax,'CurrentPoint');
x = mouse(1,1);
y = mouse(1,2);
% Make the selection point blinking
try %#ok
delete(findobj(ax,'Tag','BlinkingSquare'));
end
try %#ok
t = timerfind ('Tag','BlinkingTimer');
stop(t);
delete(t);
end
hold(ax,'on');
hr = plot(ax,x,y,'rs',...
'MarkerSize', 20, 'MarkerFaceColor', 'r', ...
'Tag','BlinkingSquare');
xlim(ax, [0 1])
ylim(ax, [0 1])
tblink = 0.25;
nblink = 15;
t = timer(...
'Name', 'BlinkingTimer',...
'ExecutionMode', 'fixedSpacing',...
'Period', tblink, ...
'StartDelay', tblink, ...
'TasksToExecute', nblink, ...
'TimerFcn', @(varargin) BlinkingToggle(hr), ...
'ErrorFcn', @(varargin) delete(hr), ...
'StopFcn', @(varargin) delete(hr), ...
'Tag', 'BlinkingTimer');
start(t);
end % Blinking
%% Make the red square toogle between visible on and off
function BlinkingToggle(hr)
if strcmp(get(hr,'Visible'),'on')
set(hr,'Visible','off');
else
set(hr,'Visible','on');
end
end
댓글 수: 2
Gavin
2024년 10월 7일
Bruno Luong
2024년 10월 7일
편집: Bruno Luong
2024년 10월 7일
I can't clearly see why timer must be specifically designed for AppDesigner, but I may be wrong.
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!