clear persistent variables in sub-functions

13 views (last 30 days)
J. Smith
J. Smith on 9 Sep 2015
Commented: Stephen23 on 14 Apr 2017
Hi,
I have a script that calls a function, which is written in separate file and contains sub-functions that are inner to main function only. In one of my sub-functions, I have persistent variable that I would like to clear every time I run the main script. How can I do so? In addition, I have breakpoints through my code, and I would prefer to keep them while I clear the persistent variable - how that can be done?
MainScript.m script:
clear variables;
y = MyMainFunction();
MyMainFunction code:
function temp = MyMainFunction()
some code...
temp = MySubFunction(var);
some code...
end
function dataOut = MySubFunction(dataIn)
persistent idx;
some code...
end
I would like to clear "idx" persistent variable every time that I run MainScript.m, but of course to keep that variable as long as the script is running.
Thanks, John

Answers (3)

Stephen23
Stephen23 on 9 Sep 2015
Edited: Stephen23 on 9 Sep 2015
The simplest method would be to clear the subfunction:
function temp = MyMainFunction()
clear('MySubFunction')
some code...
temp = MySubFunction(var);
some code...
end
Another simple method would be to introduce an special case, such as using nargin:
clear variables;
y = MyMainFunction();
function temp = MyMainFunction()
MySubFunction;
some code...
temp = MySubFunction(var);
some code...
end
function dataOut = MySubFunction(dataIn)
persistent idx;
if nargin==0
clear whatever variables here...
return
end
some code...
end
  6 Comments
Stephen23
Stephen23 on 9 Sep 2015
Edited: Stephen23 on 9 Sep 2015
@J.Smith: this is easy, just move the "reset call" to the script, instead of inside the main function as I wrote in my answer:
clear variables;
MySubFunction;
y = MyMainFunction();
function temp = MyMainFunction()
some code...
temp = MySubFunction(var);
some code...
end
Or better:
clear variables;
clear('MySubFunction')
y = MyMainFunction();
J. Smith
J. Smith on 10 Sep 2015
As I mentioned earlier, this cannot be done as the sub-function is not known at the script, but only the main-function.

Sign in to comment.


Philip Borghesani
Philip Borghesani on 9 Sep 2015
I would not use a perstent variable for this. Use a nested function and local variable
function temp = MyMainFunction()
some code...
myCommonVar=[]; %or any useful initial value
temp = MySubFunction(var);
some code...
function dataOut = MySubFunction(dataIn)
%use myCommonVar just as if it were persistent
%it will last as long as the call to MyMainFunction;
some code...
end
end
As to clearing the local function or function: in general James's analysis is correct. He missed on key point that may not be well documented. Only top level or main functions (with the same name as the file) may be cleared. To clear any local or nested function the main function must be cleared and that can't be done while the main function (or any other function in the file) is running.
  2 Comments
J. Smith
J. Smith on 10 Sep 2015
@Philip:
I prefer using local\sub function instead of nested function as the first one is more elegant use of code splitting to small tasks, as it's done in all other programming languages.
Furthermore, regarding your suggestion, myCommonVar will be reset every time I enter MyMainFunction and this is not what I want. I want to enter MyMainFunction for 5 times\iterations, and then when I run the MainScript.m again, it will start with a blank value.
About your last comment, if I understood you correctly, the reason that "clear MySubFunction" command is not working for me, is because it's called from MyMainFunction file that contains MySubFunction? So what is the workaround for this issue?
Philip Borghesani
Philip Borghesani on 10 Sep 2015
Only whole m files can be cleared from memory. The entire file is managed as a unit so sub functions can't be cleared without clearing the main function. It seems like clearing MyManFunction from MainScript as suggested previously is the correct solution. Why does this not work for you?

Sign in to comment.


James Tursa
James Tursa on 9 Sep 2015
Edited: James Tursa on 9 Sep 2015
What happens if you clear MyMainFunction? (Or clear functions)
  14 Comments
Ben Barrowes
Ben Barrowes on 14 Apr 2017
I agree rewriting a code with excellent skills would be a worthwhile goal. The difficulty is that many of my clients don't have the resources to make that goal from the beginning.
Many times, companies come to me with anything from old crufty fortran IV or f77 code to modern code that they need converted source to source into matlab, but they don't have the time or money to do a complete rewrite. What they would like is a working conversion in matlab (for a tenth or hundredth of the time and money of a rewrite) of their 10kline to 1000kline+ code rapidly and cheaply. Then their plan is to switch to matlab for that code and modify and develop the matlab code gradually going forward. I can do that for them if I can get the matlab conversion to behave like the fortran version. If I have to modify 400klines of matlab code and 3000 variables to not depend on persistence, it can amount to a rewrite which my clients do not want though maybe in a perfect world they would.
This persistent variable issue is one issue that I would like a solution to in matlab. In addition, the documentation is essentially incorrect for "clear functions." I have a workaround by adding "clear all" at the end of the main program but I would like a way to clear all persistent variables at the entrance to a m-file. I am simply asking matlab to provide a feature that they currently claim they provide in their documentation.
Stephen23
Stephen23 on 14 Apr 2017
@Ben Barrows: it sounds like you have experience to build a robust explanation and case for either submitting a bug report (incorrect documentation), or requesting an enhancement. Make this formal and see what response you get from TMW.

Sign in to comment.

Categories

Find more on Install Products in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!