A variable declared as persistent is local to the function in which you declare it; yet MATLAB retains its value in memory between calls to that function. Persistent variables are known only to the function in which you declare them. This prevents other functions or the MATLAB command line from changing the value of the persistent variable.
When you clear or modify a function that is in memory, MATLAB also clears all
persistent variables declared by that function. To keep a function in memory
until MATLAB quits, use mlock
.
MATLAB returns an error if:
You attempt to declare a variable persistent and another variable with the same name exists in the current workspace.
You specify a variable as a function input or output argument, and then try to declare that variable as persistent within the same function. For example, the following persistent declaration is invalid:
function myfun(argA)
persistent argA
For more information on variables and the workspace, see Persistent Variables.