MATLAB Answers

How to reset persistent variables in nested functions?

조회 수: 31(최근 30일)
Jan Rubak
Jan Rubak 14 Feb 2011
I can't figure out whether this is a MATLAB bug or a PEBKAC issue, but in case of the latter, I figured I'd check here first.
I have a nested function with some persistent variables. Occasionally, when some condition is met in the outer function, I wish to be able to reset these variables to their initial state (i.e. [ ]). It doesn't seem to work to use "clear MyNestedFunc" in the outer function:
function MyFunc()
MyNestedFunc();
MyNestedFunc();
clear MyNestedFunc
MyNestedFunc();
function MyNestedFunc()
persistent foo
if isempty(foo)
foo = 1;
else
foo = foo + 1;
end
disp(foo)
end
end
My desired output from this is 1, 2, 1, but what I get is 1, 2, 3.
The way to do it so that it works is apparently to make foo a persistent variable in the outer function, but for reasons of code clarity and organization I'd really prefer to encapsulate it inside the nested function if I could.
Is this a MATLAB bug because "clear MyNestedFunc" doesn't have the expected effect, or am I trying to do something in the wrong way?

  댓글 수: 0

Sign in to comment.

채택된 답변

Matt Fig
Matt Fig 14 Feb 2011
A hokey workaround:
function MyFunc()
resetfoo = false; % issued once at beginning.
MyNestedFunc();
MyNestedFunc();
MyNestedFunc();
resetfoo = true; % whenever we want to reset foo.
MyNestedFunc();
MyNestedFunc();
MyNestedFunc();
function MyNestedFunc()
persistent foo
if resetfoo || isempty(foo)
foo = 1;
resetfoo = false;
else
foo = foo + 1;
end
disp(foo)
end
end

  댓글 수: 1

Jan Rubak
Jan Rubak 14 Feb 2011
Hi Matt, Thanks. That's actually a better workaround than what I'd been able to come up with, since the resetfoo variable name can refer to the nested function in general rather than the specific (internal) variables. Rereading the doc page it really seems like my original code ought to be the "right" approach, so this still feels like a workaround, but I'll take it! Cheers.

Sign in to comment.

추가 답변(1개)

Walter Roberson
Walter Roberson 14 Feb 2011
The documentation indicates somewhere that persistent variables are associated with the parsed version of file, not with the workspace, and are reset when the file as a whole is reloaded.

  댓글 수: 3

Jan Rubak
Jan Rubak 14 Feb 2011
Hi Walter, Thanks for your reply. I don't see any indication of that on the doc page for "persistent" or in the programming fundamentals section of the users guide. I don't suppose you remember where you saw that, or you would've already said? If what you suggest is true, then I'd run into the same problem when using a subfunction instead of a nested function. (Checks... yep.) Hmm, that actually seems even more problematic, because the workaround there would presumably be to make foo a global variable. I'm trying to encapsulate data, not spread it all over my workspace scope.
Walter Roberson
Walter Roberson 14 Feb 2011
This is a matter that I "read between the lines" from the description of "clear" and "rehash".
Jan Rubak
Jan Rubak 14 Feb 2011
Ah, I see. Well kudos to you for picking up on it, and thanks for letting me know. I think I'll pass this on to Mathworks as a possible bug, then. Cheers.

Sign in to comment.


Translated by