# Doug's MATLAB Video Tutorials

## Persistent variables in MATLAB

There are situations where you want MATLAB to remember the value of a variable from one call of a function to another call of that function. We show to use a persistent value to accomplish this. This approach is convenient when calling the same function from several different places, or in callbacks, such as when coding an app or a timer.

### 7 Responses to “Persistent variables in MATLAB”

1. Chris replied on :

Doug:

This is a great tip! Another great Matlab feature I did not know existed. Thanks!

One thing: If you run main.m consecutively, count continues to increment with each run. I.e., In the first run, count goes from 1-10; in the second it goes from 11 – 20, and so on. How can you “reset” the persistent variable to a default value the first time it is called from MAIN during separate runs?

2. Doug replied on :

@Chris,

You will have to have main reset it the first time. Set a flag somewhere, or pass in a flag to reset the persistent variable with.

Doug

3. Rauly replied on :

How can I used a for loop to make all the even columns of a matrix zero in matlab?
I have to define a matrix and then make all the even columns zero, ,this it’s what i have so far:

function [A] = evencolzero(~, ~, ~)
%This function take a matrix and make zero all even columns.
% This program will take a matrix called Matrix M of any dimension and
% then it will make all even columns zero and finally it will display both
% of them, the original and the Matrix A.
M= [1 2 3 4 2 5 4 8; 5 6 7 8 3 5 8 9; 9 10 11 12 13 5 8 96];
X=M; %it makes a copy of the original matrix and store it in the variable X
M(:, 2:2:N)=zeros; %make all the even columns zero
A=M; %stores the new matrix in a variable called A
disp (‘The original matrix M is:’);
disp (”);
disp (X);
disp (‘The matrix A is:’);
disp (A);

end

The problem is that N needs to be define as the number of columns, i was thinking that maybe with a for loop like this j=2:2:N it will scan the whole matrix but i have no idea how to count the numbers of columns and assign it to N, any ideas?

4. doug replied on :

>> a = magic(14);
>> a(:,2:2:end)=0

a =

177 0 195 0 10 0 28 0 137 0 99 0 68 0
185 0 154 0 18 0 29 0 145 0 107 0 76 0
193 0 155 0 26 0 37 0 104 0 115 0 84 0
5 0 163 0 34 0 45 0 112 0 123 0 85 0
160 0 171 0 42 0 4 0 113 0 131 0 93 0
168 0 179 0 43 0 12 0 121 0 139 0 52 0
169 0 187 0 2 0 20 0 129 0 147 0 60 0
30 0 48 0 157 0 175 0 88 0 50 0 117 0
38 0 7 0 165 0 176 0 96 0 58 0 125 0
46 0 8 0 173 0 184 0 55 0 66 0 133 0
152 0 16 0 181 0 192 0 63 0 74 0 134 0
13 0 24 0 189 0 151 0 64 0 82 0 142 0
21 0 32 0 190 0 159 0 72 0 90 0 101 0
22 0 40 0 149 0 167 0 80 0 98 0 109 0

5. Nabihah replied on :

Hi, so I’m trying to learn how to use persistent variables.

where kriging1 is a function that calculates all those variables (k, n, one, etc) that I’m trying to keep as persistent variables.

I then want to use the persistent variables in this function:

This function is called by another function. Unfortunately, it keeps saying that kriging_params.n is undefined when I try to run my mainfile. Any idea how to call kriging_params.(variables)?

Thanks

6. Nabihah replied on :

Apparently my code didn’t past in my last comment. Sorry about that:

Hi, so I’m trying to learn how to use persistent variables.

function compute_kriging_parameters(x,Y)
persistent kriging_params;
if isempty(kriging_params)
[kriging_params.k, kriging_params.n, kriging_params.one, kriging_params.y, kriging_params.U,
kriging_params.mu, kriging_params.p, kriging_params.X, kriging_params.logtheta, kriging_params.sig] = kriging1 (x,Y);
end

where kriging1 is a function that calculates all those variables (k, n, one, etc) that I’m trying to keep as persistent variables.

I then want to use the persistent variables in this function:

function val = PredictAPoint(X, y, params)
x = X;
Y = y;
Xpredict = [params.parameter1(1) params.parameter2(1)];
compute_kriging_parameters(x,Y)
psi=zeros(kriging_params.n,1);
theta=10.^kriging_params.logtheta;
for i=1:n
psi(i,1)=corYYND2(Xpredict,kriging_params.X(i,:),theta,kriging_params.p,kriging_params.k);
end
val = kriging_params.mu+psi’*(kriging_params.U\(kriging_params.U’\(kriging_params.y-kriging_params.one*kriging_params.mu)));
end

This function is called by another function. Unfortunately, it keeps saying that kriging_params.n is undefined when I try to run my mainfile. Any idea how to call kriging_params.(variables)?

Thanks!

7. Doug replied on :

It looks like you are using kriging_params.mu as a structure with a field name of mu. It is a function, so that is the source of the confusion.

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 1 + 5 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

These postings are the author's and don't necessarily represent the opinions of MathWorks.