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?
채택된 답변
추가 답변 (5개)
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
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
2021년 1월 1일
Confirmed. Real headache. The behavior of local functions with persistent variables is random.
Shubham Baisthakur
2021년 5월 3일
Can you please share the command syntax?
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
2021년 5월 4일
Thanks Walter!
This works
will wehner
2014년 1월 8일
%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
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
2013년 12월 5일
you can use:
clear YourVariableName
see for more information:
댓글 수: 3
Daniel
2013년 12월 5일
sixwwwwww
2013년 12월 5일
Maybe you can try the following commands:
Vars=whos;
PersistentVars=Vars([Vars.persistent]);
PersistentVarNames={PersistentVars.name};
clear(PersistentVarNames{:});
will wehner
2014년 1월 8일
Doesnt Work!
morteza
2015년 1월 23일
0 개 추천
you can use each of these instructions: clear all or clear classes or clear functions
Viktor Svensson
2016년 6월 27일
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 Exchange에서 Variables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!