How to clear persistent variables?

305 visualizaciones (últimos 30 días)
Daniel
Daniel el 5 de Dic. de 2013
Comentada: Shubham Baisthakur el 4 de Mayo de 2021
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?

Respuesta aceptada

Yair Altman
Yair Altman el 5 de Dic. de 2013
Editada: Yair Altman el 5 de Dic. de 2013
you can use
clear functionName
and all the persistent vars in that function will be cleared (set to [])
  6 comentarios
Alexander Ryan
Alexander Ryan el 27 de Abr. de 2019
So is there a way to clear persistent variables in local or nested functions?

Iniciar sesión para comentar.

Más respuestas (5)

TADA
TADA el 27 de Abr. de 2019
Editada: TADA el 27 de Abr. de 2019
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 comentarios
Walter Roberson
Walter Roberson el 4 de Mayo de 2021
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 el 4 de Mayo de 2021
Thanks Walter!
This works

Iniciar sesión para comentar.


will wehner
will wehner el 8 de En. de 2014
%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 comentario
Image Analyst
Image Analyst el 8 de En. de 2014
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

Iniciar sesión para comentar.


sixwwwwww
sixwwwwww el 5 de Dic. de 2013
you can use:
clear YourVariableName
see for more information:
  3 comentarios
sixwwwwww
sixwwwwww el 5 de Dic. de 2013
Maybe you can try the following commands:
Vars=whos;
PersistentVars=Vars([Vars.persistent]);
PersistentVarNames={PersistentVars.name};
clear(PersistentVarNames{:});
will wehner
will wehner el 8 de En. de 2014
Doesnt Work!

Iniciar sesión para comentar.


morteza
morteza el 23 de En. de 2015
you can use each of these instructions: clear all or clear classes or clear functions

Viktor Svensson
Viktor Svensson el 27 de Jun. de 2016
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.

Categorías

Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by