PERSISTENT Persistent Variables

Section: Variables and Arrays

Usage

Persistent variables are variables whose value persists between calls to a function or script. The general syntax for its use is
   persistent variable1 variable2 ... variableN

The persistent statement must occur before the variable is the tagged as persistent. Per the MATLAB API documentation an empty variable is created when the persistent statement is called.

Example

Here is an example of a function that counts how many times it has been called.

     count_calls.m
function count_calls
  persistent ccount
  if (~exist('ccount')) ccount = 0; end;
  ccount = ccount + 1;
  printf('Function has been called %d times\n',ccount);

We now call the function several times:

--> for i=1:10; count_calls; end
Function has been called 0 times
Function has been called 0 times
Function has been called 0 times
Function has been called 0 times
Function has been called 0 times
Function has been called 0 times
Function has been called 0 times
Function has been called 0 times
Function has been called 0 times
Function has been called 0 times