如何在MATLAB方法中清除持久变量 [英] How to clear a persistent variable in a MATLAB method
问题描述
我有一个MATLAB类,其中包含采用持久变量的方法.当满足某些条件时,我需要清除持久变量而不清除该方法所属的对象.我已经能够做到这一点,但是只能通过使用clear functions
来实现我的目的.
此问题的classdef .m文件:
classdef testMe
properties
keepMe
end
methods
function obj = hasPersistent(obj)
persistent foo
if isempty(foo)
foo = 1;
disp(['set foo: ' num2str(foo)]);
return
end
foo = foo + 1;
disp(['increment foo: ' num2str(foo)]);
end
function obj = resetFoo(obj)
%%%%%%%%%%%%%%%%%%%%%%%%%
% this is unacceptably broad
clear functions
%%%%%%%%%%%%%%%%%%%%%%%%%
obj = obj.hasPersistent;
end
end
end
使用此类的脚本:
test = testMe();
test.keepMe = 'Don''t clear me bro';
test = test.hasPersistent;
test = test.hasPersistent;
test = test.hasPersistent;
%% Need to clear the persistent variable foo without clearing test.keepMe
test = test.resetFoo;
%%
test = test.hasPersistent;
test
输出为:
>> testFooClear
set foo: 1
increment foo: 2
increment foo: 3
increment foo: 4
set foo: 1
test =
testMe
Properties:
keepMe: 'Don't clear me bro'
Methods
这是所需的输出.问题是classdef文件中的行clear functions
清除了内存中的所有功能.我需要一种清除范围较小的方法.例如,如果hasPersistent' was a function instead of a method, the appropriately scoped clear statement would be
clear hasPersistent`.
我知道clear obj.hasPersistent
和clear testMe.hasPersistent
都无法清除持久变量. clear obj
同样是个坏主意.
在评论中进行讨论之后,我认为您想将make foo
用作私有属性,并附带适当的increment
/reset
公共功能
I have a MATLAB class that contains a method that employs a persistent variable. When certain conditions are met I need to clear the persistent variable without clearing the object to which the method belongs. I've been able to do this, but only by employing clear functions
which has excessively broad scope for my purposes.
The classdef .m file for this problem:
classdef testMe
properties
keepMe
end
methods
function obj = hasPersistent(obj)
persistent foo
if isempty(foo)
foo = 1;
disp(['set foo: ' num2str(foo)]);
return
end
foo = foo + 1;
disp(['increment foo: ' num2str(foo)]);
end
function obj = resetFoo(obj)
%%%%%%%%%%%%%%%%%%%%%%%%%
% this is unacceptably broad
clear functions
%%%%%%%%%%%%%%%%%%%%%%%%%
obj = obj.hasPersistent;
end
end
end
A script that employs this class:
test = testMe();
test.keepMe = 'Don''t clear me bro';
test = test.hasPersistent;
test = test.hasPersistent;
test = test.hasPersistent;
%% Need to clear the persistent variable foo without clearing test.keepMe
test = test.resetFoo;
%%
test = test.hasPersistent;
test
The output from this is:
>> testFooClear
set foo: 1
increment foo: 2
increment foo: 3
increment foo: 4
set foo: 1
test =
testMe
Properties:
keepMe: 'Don't clear me bro'
Methods
which is the desired output. The problem is that the line clear functions
in the classdef file clears all functions in memory. I need a way to clear with a much smaller scope. For example, if hasPersistent' was a function instead of a method, the appropriately scoped clear statement would be
clear hasPersistent`.
I know that clear obj.hasPersistent
and clear testMe.hasPersistent
both fail to clear the persistent variable. clear obj
is similarly a bad idea.
Following the discussion in the comments, I think you want to use make foo
a private property, accompanied with appropriate increment
/reset
public functions.
这篇关于如何在MATLAB方法中清除持久变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!