clear persistent variables in sub-functions

13 次查看(过去 30 天)
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

回答(3 个)

Stephen23
Stephen23 2015-9-9
编辑:Stephen23 2015-9-9
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 个评论
Stephen23
Stephen23 2015-9-9
编辑:Stephen23 2015-9-9
@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 2015-9-10
As I mentioned earlier, this cannot be done as the sub-function is not known at the script, but only the main-function.

请先登录,再进行评论。


Philip Borghesani
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 个评论
J. Smith
J. Smith 2015-9-10
@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 2015-9-10
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?

请先登录,再进行评论。


James Tursa
James Tursa 2015-9-9
编辑:James Tursa 2015-9-9
What happens if you clear MyMainFunction? (Or clear functions)
  14 个评论
Ben Barrowes
Ben Barrowes 2017-4-14
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 2017-4-14
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Install Products 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by