6

How does one list/view/clear persistent variables in MATLAB? I'd like to see persistent variables not for a particular function, but for all functions that have persistent variables currently in memory.

I've tried things like whos('persistent') and whos('global') with no luck.

horchler
  • 18,384
  • 4
  • 37
  • 73
Nick
  • 167
  • 2
  • 12
  • 1
    I'm not sure what you're trying to do. Persistent variables can only be declared inside a function so you'll need to call `whos` in there. Once the variable exists, you'll see that `whos` shows that it is `'persistent'` under the `'Attributes'` column. – horchler Nov 05 '13 at 22:50
  • Thanks for your reply horchler. I am looking for a way to list all functions which have persistent variables in memory, and list their persistent variables. I realize that you can use clear as you described below to clear persistent variables, but this would require you to know beforehand which functions have persistent variables. – Nick Nov 06 '13 at 16:17
  • 2
    I'm not sure there's a way. You might contact The MathWorks and put in a service request. One potentially useful function is [`inmem`](http://www.mathworks.com/help/matlab/ref/inmem.html), but it will just tell you which functions, mex files, and classes have been compiled and loaded into memory. I suppose you could use it as a starting point and parse the function names returned looking for `persistent` (maybe with `mlintmex('-lex','filename.m')`), but that's quite a hack. – horchler Nov 06 '13 at 17:16

1 Answers1

11

If you want to clear a persistent from outside of the function within which it's defined, then you need to clear the function itself:

clear functionNameWithPersistentVariable

Or clear all (unlocked) functions from memory:

clear functions

If the function in question is actually a method of a class, you may need to use clear classes instead. See also this table in the documentation for clear.

Within the function itself you may be able to use whos and something like the suggestion in this Matlab Central answer. Unfortunately, I don't know of any elegant documented way to find or list functions or persistent variables that are currently in memory.

horchler
  • 18,384
  • 4
  • 37
  • 73