Best method to clear persistent variables

188 visualizaciones (últimos 30 días)
Gurudatha Pai
Gurudatha Pai el 26 de Sept. de 2011
Respondida: John D'Errico el 4 de Sept. de 2022
Hello all,
Recently I came to know that it is not a good idea to use
clear all;
So, I started using
clear variables
Now, I am wondering what I should do to clear only the persistent variables that I use inside my functions. It is absolutely critical that I clear the persistent states of my adaptive algorithms. Is there a way I can clear only persistent variables so that I don't have to re-run the entire script to generate the same data only to be used in a differently initialized algorithm?
Currently, I do this by calling the particular function with a
myfunction('init', init_method)
to clear the persistent variables or re-initialize them. Is it the best way? Your comments will help,
Thank you,

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 26 de Sept. de 2011
When you use Vars=whos, it returns flag indicating global variable and persistent variable. You can use that to pick up persistent variables to clear them. The example below uses 'global' because it's easy for me. You can try it replacing [Vars.global] with [Vars.persistent].
Vars=whos;
PersistentVars=Vars([Vars.global]);
PersistentVarNames={PersistentVars.name};
clear(PersistentVarNames{:});
  6 comentarios
Kenneth Leung
Kenneth Leung el 23 de Feb. de 2017
Editada: Kenneth Leung el 23 de Feb. de 2017
Persistent variables are only visible within the function itself, so I like to write an 'if' statement at the beginning of the called function
input.flush = true;
if input.flush
vars = whos;
vars = vars([vars.persistent]);
varName = {vars.name};
clear(varName{:});
output = [];
return
end
This way I have more reliably been able to 'flush' the persistent variables away whenever I want to by setting flush to true and calling my function again.
Walter Roberson
Walter Roberson el 23 de Feb. de 2017
Yes but this transfers the problem to be how to set input.flush from outside of the function.
When persistent variables are used to represent ordinary state (e.g. usage statistics,) that are not big, then postponing the clear until the next call to the function can be ok. But persistent is also used for cache and when you are finished with the cache or it has gotten too big then you would like to clear it without calling the function. Even if the memory directly used is small, it could be handles to large objects and you might need to clear the reference to release the objects.

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 26 de Sept. de 2011
I don't know about the "best" way, but you can clear all the persistent variables in a particular function by asking to clear the function itself; e.g.,
clear myfunction
This will probably have the side effect of removing the stored JIT'd copy of it, causing it to be reparsed the next time it is invoked.
  6 comentarios
Gavriel Aminov
Gavriel Aminov el 6 de En. de 2021
Editada: Walter Roberson el 4 de Sept. de 2022
@Leo Simon,
Leo, you are right, that the problem must be solved at the parent of the calling function, I thank you.
I just want to stress the point, that I got the desirable result of clearing the persistent variable by the next way of:
clearing the function, the persistent variable is declared in, at the parent function of the calling function.
That is:
function FunParent()
clear FunWithPersistent
[a,b,c]=FunCalling();
end
------------------------------------
function FunCalling()
[x,y]=FunWithPersistent();
end
------------------------------------
function FunWithPersistent()
persistent OurHero
...
end
I'm sorry to say, that it is indeed seems to be a bug of MATLAB...
Dimitrios Patikas
Dimitrios Patikas el 4 de Sept. de 2022
Thank you

Iniciar sesión para comentar.


John D'Errico
John D'Errico el 4 de Sept. de 2022
You can also use
clear persistent
Which will clear all persistent variables.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by