Does matlab lock my function in memory such that I can't call clear funcName to clear persistent variables?

I have a script in which I am calling this function, defined in its own file (mdl5Func, see first block below) and then I'm calling it in another function file (see second block). Before I call it inside this second function, I do 'clear mdl5Func' to clear persistent variables (what I'm trying to do is make the variables persist within one call (to a function searchlight3D, which itself calls mdl5Func) but not persist between calls to searchlight3D. From the matlab persistent variable page, it seems like if I have mdl5Func defined in its own function file, calling clear on it should clear the persistent variables inside. I have a feeling this might not be working because of the way its getting passed one function as part of another, anonymous function, but I don't know this for sure, so am looking for confirmation that matlab locks functions loaded in memory in this way/ the way in which I'm misunderstanding how clear works.
function [z] = mdl5Func(M,imgPerm)
persistent prev_imgPerm S IX;
if isempty(prev_imgPerm) || isempty(S) || isempty(IX) || ...
~isequal(prev_imgPerm, imgPerm)
% Construct the basic 6x6 similarity hypothesis
simFun = @(x,y) 1-((min(mod((x-y),6),mod((y-x),6))/3));
H_ = nan(6);
for ii = 1:36
[x,y] = ind2sub([6,6],ii);
H_(ii) = simFun(x,y);
end
h = H_(tril(true(6),-1));
% Make the selectors for the real data RSM
S.n.ab = logical(kron([0,0;1,0],tril(true(6),-1)));
S.n.ba = logical(kron([0,0;1,0],triu(true(6),1)));
S.p.a = logical(kron([1,0;0,0],tril(true(6),-1)));
S.p.b = logical(kron([0,0;0,1],tril(true(6),-1)));
% Load in predicted visual similarity from denseNet-169
V = getVisCorr();
% Permute the rows and cols of V
PV = V(imgPerm,imgPerm);
%select out lower tri
v = PV(tril(true(6),-1));
%scale all vectors such that they are projecting from the origin
%and are zscored
h = zscore(h);
h = repmat(h,[2,1]);
v = zscore(v);
v = repmat(v,[4,1]);
% Construct the design matrix (X)
X = [kron(eye(2),h),v];
% Invert for regression
IX = pinv(X);
% Set/update prev_imgPerm
prev_imgPerm = imgPerm;
end
%get the 12x12 RSM for the searchlight image
R = corr(M);
%% prep y column of design mat
%select out colcoation based vals of R and zscore within aa/bb/ba comp type
yn = zscore([R(S.n.ab);R(S.n.ba)]);
yp.a = zscore(R(S.p.a));
yp.b = zscore(R(S.p.b));
y = [yn;yp.a;yp.b];
%do regression and Fischer-transform the results
z = atanh(IX*y);
return
block 2: seperate function file
%...bunch of stuff....
clear mdl5Func
%....bunch of stuff above
[Z,N] = searchlight3D(...
r,... Radius -
@(M) mdl5Func(M,imgPerm),... Function
Mask,... Mask
Timgs,... Data
Ydepth,... Output depth
char(cSid));

3 Comments

First, if comment out the call to searchlight3D from the second, are the persistent variables then cleared? We don't have all context, but that appears should work to do that as expected from what can see.
I think it may be possible if you'll change the second function such that
...
fnMdl=@(M) mdl5Func(M,imgPerm);
Z,N] = searchlight3D(...
r, ... Radius -
fnMdl, ... Function
Mask, ... Mask
Timgs, ... Data
Ydepth, ... Output depth
char(cSid));
...
Then the function is redefined on execution rather than being parsed and the handle created when the function is read into memory and cached. No promises, but...
So I tried doing what I think you are suggesting with this sentence 'comment out the call to searchlight3D from the second, are the persistent variables then cleared' and the persistent variables were still uncleared, but whilst doing this thought to check and it looks like maybe the fact that I'm pausing the scripts and going in and out of the stack is what stopping the clear from working ?
Side note: so I did try doing what you've suggested with the code section but found that if you assing the anonymous function to a handle and then pass the handle to searchlight3D() it errors inside searchlight3D saying that imgPerm is not recogised as a variable.
I appreciate that there is probably a lot of context missing I just thought it was unfeasible to post all three function scripts in full on here. Anyway, thank you for your help!
"...to a handle and then pass the handle to searchlight3D() it errors inside searchlight3D saying that imgPerm is not recogised as a variable. "
Yes, you'll either have to pass the second array as an argument or use alternate techniques to share parameters. There are examples of how to do this with the ODE solvers.
When an anonymous function is defined, anything not in the argument list is replaced by the value of the variable at that time and cached permanently until the anonymous function is redefined. That means that if the function handle is defined once and some auxiliary data it references changes, the evaluation of the function won't reflect that change if it isn't redefined.
x=3;
fn=@(y)y+x;
fn(2)
ans = 5
x=0; % change x
fn(2) % answer doesn't change
ans = 5
fn=@(y)y+x; % redefine fn
fn(2) % now answer does change, too...
ans = 2

Sign in to comment.

Answers (1)

I made a simple example and tested it. "clear FunName" does clear the persistent variable.
clear all;
test;
function output=test
output=AddByOne
output=AddByOne
clear AddByOne
output=AddByOne
output=AddByOne
end
>> test;
output =
1
output =
2
output =
1
output =
2
Need to make the following a separate function. local function or nested function don't behave the same.
function count=AddByOne
persistent Memory
if isempty(Memory)
Memory=1;
else
Memory=Memory+1;
end
count=Memory;
end

Categories

Products

Release

R2024b

Asked:

on 24 Feb 2026

Edited:

dpb
on 24 Feb 2026

Community Treasure Hunt

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

Start Hunting!