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?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? 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? delete timer MATLAB Answers — New Questions