How can I clear persistent variables from a function using only its handle?

5 ビュー (過去 30 日間)
John Billingham
John Billingham 2017 年 9 月 28 日
コメント済み: Guillaume 2017 年 9 月 28 日
For example, say I have a simple function, test1,
function test1 = test1(x,y)
persistent k
if isempty(k), k = sum(y); end
test1 = k*x*y;
which I call from another function, test0, within which I want to clear the persistent variable, k, in test1, for example,
function test0 = test0(x,y,fn)
clear fn
for i = 1:length(x)
test0 = fn(x(i),y);
end
using
test0(1,2,@test1)
But, of course, it doesn't work, with error
Reference to a cleared variable fn.
Error in test0 (line 4)
test0 = fn(x(i),y);
because the line
clear fn
doesn't do the job, for obvious reasons, but I don't know how to fix this.
Thanks in advance for your help.
  1 件のコメント
Stephen23
Stephen23 2017 年 9 月 28 日
編集済み: Stephen23 2017 年 9 月 28 日
function test1 = test1(x,y)
Using the same name for the output variable and the function is not a good idea. I am surprised that this even works. I would recommend that you change the names so that they are not the same.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2017 年 9 月 28 日
編集済み: Guillaume 2017 年 9 月 28 日
Easy:
clear(func2str(yourfunctionhandle))
Note that from a design perspective, you'd be better off with a class rather than a function that holds state, particularly if that state is controlled from different places as you're trying to do.

その他の回答 (1 件)

Steven Lord
Steven Lord 2017 年 9 月 28 日
Another approach would be for the function you created that uses a persistent variable to accept a special flag, one that isn't a normal data input, that tells it to clear the persistent variable.
function z = addXToY(x)
persistent y
if isempty(y)
y = 0;
end
if isequal(x, 'reset')
z = y;
y = 0;
else
y = y + x;
z = y;
end
Call it like:
>> z = addXToY(1)
z =
1
>> z = addXToY(2)
z =
3
>> z = addXToY(3)
z =
6
>> z = addXToY('reset')
z =
6
>> z = addXToY(4)
z =
4
>> z = addXToY(5)
z =
9
  2 件のコメント
John Billingham
John Billingham 2017 年 9 月 28 日
Yes, I thought of that, but it's not as elegant as just being able to clear the function in one line, external to the function, simply using its name.
Thanks for thinking about it though.
Guillaume
Guillaume 2017 年 9 月 28 日
As I said in my answer, a function that has state and special flags (that don't come with tab completion) would be better implemented as a class (that supports tab completion):
classdef myfunctionobject < handle
properties (access = private)
state;
end
methods
function this = myfunctionobject()
this.Reset();
end
function Reset(this))
this.state = 0;
end
function result = DoSomething(this, x, y)
this.state = this.state + 1;
result = x + this.state * y;
end
end
end
Used:
fn = myfunctionobject; %equivalent to setting the function handle
fn.Reset; %equivalent to the clear call
fn.Dosomething(x, y); %equivalent to calling the function handle

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by