How to delete persistent variables in a timer fcn

Hi all,
I am using 3 persistent variables in a timer callback function. If I close the GUI (CloseRequestFcn) I want to delete the variable so that it is reinitialized if I start the programm again.
Actually I tried
clear timer_callback_fcn;
But that doesn't work. Moreover I tried
clear functions
This is working well but it doesn't seem to be the best way. Any ideas to improve?
Thank you!

2 件のコメント

Jan
Jan 2013 年 9 月 23 日
Please explain "does not work" with any details.
Adrian
Adrian 2013 年 9 月 23 日
編集済み: Adrian 2013 年 9 月 23 日
"does not work" means that the values of the variables are not deleted if I restart the timer

サインインしてコメントする。

 採用された回答

Jan
Jan 2013 年 9 月 23 日

0 投票

Is timer_callback_fcn an M-file, a subfunction or a nested function? You can clear M-files only.
Instead of persistent variables, storing the data in the timer's UserData looks smarter. Then starting teh timer/GUI again, the data are reset automatically.

2 件のコメント

Adrian
Adrian 2013 年 9 月 23 日
How can I store it in the timers UserData?
Adrian
Adrian 2013 年 9 月 23 日
編集済み: Adrian 2013 年 9 月 23 日
Did it: (This is part of the timer_callback_fcn)
if ~isempty(get(obj, 'UserData'))
t=get(obj, 'Userdata');
delete(t.t1);
delete(t.t2);
delete(t.tred);
end
t.t1 = text(...);
t.t2 = text(...);
t.tred = text(...);
set(obj, 'UserData', t);
Thank you for helping me...!

サインインしてコメントする。

その他の回答 (1 件)

Robert Cumming
Robert Cumming 2013 年 9 月 23 日

0 投票

if you want to keep your current methodology you need to add a "clear" option to your timer callback function which gets called when the gui closes.
i.e.
function myTimerCallback ( varargin )
persistent myTime
if isempty ( myTime ); myTime = now(); end % initialise
% reset the timer when the function is called with
% myTimerCallback ( 'clear' )
if nargin == 1 && ischar ( varargin{1} )
if strcmp ( varargin{1}, 'clear' )
myTime = [] % or however you want to reset.
return
end
end
% your code goes here
end
The use of persistent variables to store GUI data becomes even more complex if you have more than 1 GUI active at a time..... It is possible, but not easy.
As Jan said it might be easier to store the information in the timer function itself (depends on how you use the callback).

1 件のコメント

Adrian
Adrian 2013 年 9 月 23 日
plausible solution, thak you!
But currently I am working with more than 1 GUI. That's why I will try to store it in timers userdata first.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

質問済み:

2013 年 9 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by