How to clear persistent variables?

I am currently using a persistent variable within a function that I would like to clear at the end of the execution of a larger program. I am currently using "clear functions". This is causing some headaches during debugging since this clears my breakpoints as well. Is there a way to clear only the persistent variables? Better yet, is there a way to clear specific persistent variables?

 採用された回答

Yair Altman
Yair Altman 2013 年 12 月 5 日
編集済み: Yair Altman 2013 年 12 月 5 日

10 投票

you can use
clear functionName
and all the persistent vars in that function will be cleared (set to [])

6 件のコメント

Image Analyst
Image Analyst 2013 年 12 月 5 日
I suppose if you had function1() and function2() that used persistent variables, and you were using GUIDE, then in the OutputFcn() (which gets run both on startup, and again on shutdown), you'd put these lines:
clear function1;
clear function2;
and then next time you run the program, and it enters either function1 or function2, they won't have any values from the prior run of the program still "leftover" in the persistent variables of those functions.
will wehner
will wehner 2014 年 1 月 8 日
Doesn't work.
mcsefl
mcsefl 2015 年 12 月 4 日
編集済み: mcsefl 2015 年 12 月 4 日
My experience:
I found that "clear functionName" can in fact be used to clear persistent variables effectively.
However, I noticed a difference in behavior if it is used in a local function versus a function that is saved in a separate file. This approach does not seem to work if you are trying to clear a local function, if implemented as written above.
Here is minimal example to demonstrate the difference in behavior. The function "testFunc" is a local function, and there is an identical copy of testFunc that is saved in a separate file and re-named "testFunc_separateFile".
function clearTest()
clear testFunc
disp('testFunc output:')
for ii = 1:2
testFunc(ii)
end
clear testFunc_separateFile
disp('testFunc_separateFile output:')
for ii = 1:2
testFunc_separateFile(ii)
end
end
% Local Function
function testFunc(arg)
persistent counter
if isempty(counter)
counter = 0;
end
counter = counter + arg
end
If I run the "clearTest" twice, here is the output:
>> clearTest
testFunc output:
counter =
1
counter =
3
testFunc_separateFile output:
counter =
1
counter =
3
>> clearTest
testFunc output:
counter =
4
counter =
6
testFunc_separateFile output:
counter =
1
counter =
3
Vineeth Nair
Vineeth Nair 2018 年 12 月 12 日
The clear function does not clear persistent variables in local or nested functions.
Alexander Ryan
Alexander Ryan 2019 年 4 月 27 日
So is there a way to clear persistent variables in local or nested functions?
Gavriel Aminov
Gavriel Aminov 2021 年 1 月 6 日

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

その他の回答 (5 件)

TADA
TADA 2019 年 4 月 27 日
編集済み: TADA 2019 年 4 月 27 日

2 投票

You can clear the name of the .m file in which that function is declared
it works for .m file functions, local functions, nested functions, class methods
it also works if you kept a function handle to that function

5 件のコメント

Walter Roberson
Walter Roberson 2019 年 4 月 27 日
Right. What you cannot do is clear persistent variables in an individual function declared inside a file. "clear" with a file name works by discarding the JIT structure for the file (which includes the persistent variables), but local and nested functions have their JIT as part of the enclosing file and cannot be individually cleared.
Dariusz Borkowski
Dariusz Borkowski 2021 年 1 月 1 日
Confirmed. Real headache. The behavior of local functions with persistent variables is random.
Shubham Baisthakur
Shubham Baisthakur 2021 年 5 月 3 日
Can you please share the command syntax?
Walter Roberson
Walter Roberson 2021 年 5 月 4 日
clear NAME
where NAME is the name of a file that contains the function with the persistent variable you want to clear, without file extension. For example
clear project17
for file project17.m
There is no way to clear selectively within a file.
Shubham Baisthakur
Shubham Baisthakur 2021 年 5 月 4 日
Thanks Walter!
This works

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

will wehner
will wehner 2014 年 1 月 8 日

1 投票

%Example Function
function testCleanup
disp(' ')
k = 3;
myFun;
for i=1:2\n
myFun
end
function myFun
persistent x
if(isempty(x))
x=1;
disp('empty')
else
disp('not empty')
end
end
end %end testCleanup
The function myFun is called 3 times. Once, then 2 more times in a loop. When running this function for the first time it will display
empty
not empty
not empty
on each subsequent run of testCleanup, it displays
not empty
not empty
not empty
It is my desire to clear the persistent variable x at the end of the function testCleanup. Simply clearing the function does not work. Using the whos command outside the function scope does not work. Is there any way to use onCleanup to clear the persistent variable x?

1 件のコメント

Image Analyst
Image Analyst 2014 年 1 月 8 日
Try this:
function testCleanup
clc;
k = 3;
myFun;
for i=1:2
myFun
end
clear functions;
function myFun
persistent x
if(isempty(x))
x=1;
disp('empty')
else
disp('not empty')
end
end
end %end testCleanup

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

sixwwwwww
sixwwwwww 2013 年 12 月 5 日

0 投票

you can use:
clear YourVariableName
see for more information:

3 件のコメント

Daniel
Daniel 2013 年 12 月 5 日
Thanks for the response. Unfortunately, this does not work for persistent variables that are out of scope. For example, if you declare a persistent variable in a subfunction you cannot call clear from the base workspace to clear it.
sixwwwwww
sixwwwwww 2013 年 12 月 5 日
Maybe you can try the following commands:
Vars=whos;
PersistentVars=Vars([Vars.persistent]);
PersistentVarNames={PersistentVars.name};
clear(PersistentVarNames{:});
will wehner
will wehner 2014 年 1 月 8 日
Doesnt Work!

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

morteza
morteza 2015 年 1 月 23 日

0 投票

you can use each of these instructions: clear all or clear classes or clear functions
Viktor Svensson
Viktor Svensson 2016 年 6 月 27 日

0 投票

Ran into this problem today, wanted to clear persistent variables and not the debug points. The best I could come up with was
debugStatus = dbstatus(fileName);
clear(fileName);
dbstop(debugStatus)
where dbstatus reads the debug points in the file and dbstop returns them after the clear.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by