如何获取MATLAB类中的静态成员变量?
有没有办法在 MATLAB 类中定义静态成员变量?
这不起作用:
classdef A
properties ( Static )
m = 0;
end
end
建议使用关键字“Constant”而不是“Static”,常量属性无法修改。我想要一个对于类 A
的所有对象通用的变量,并且我希望能够在类 A
的方法中修改该变量。
所以我需要的是一个私有静态成员变量。有没有办法在MATLAB中获取它?
发现可以使用静态成员函数中的持久变量来完成解决方法。
在这种情况下,您应该从基类继承所有类,如下所示。
classdef object < handle
properties ( GetAccess = 'public', SetAccess = 'private' )
id
end
methods ( Access = 'protected' )
function obj = object()
obj.id = object.increment();
end
end
methods ( Static, Access = 'private' )
function result = increment()
persistent stamp;
if isempty( stamp )
stamp = 0;
end
stamp = stamp + uint32(1);
result = stamp;
end
end
end
Is there a way to define static member variables in MATLAB classes?
This doesn't work:
classdef A
properties ( Static )
m = 0;
end
end
It suggests to use keyword "Constant" instead of "Static", the constant properties cannot be modified. I want a variable common to all objects of class A
and I want to be able to modify that variable in methods of class A
.
So what I need is a private static member variable. Is there a way to obtain it in MATLAB?
Found out that a workaround can be done using persistent variables in static member functions.
In this case you should inherit all your classes from a base class like the following.
classdef object < handle
properties ( GetAccess = 'public', SetAccess = 'private' )
id
end
methods ( Access = 'protected' )
function obj = object()
obj.id = object.increment();
end
end
methods ( Static, Access = 'private' )
function result = increment()
persistent stamp;
if isempty( stamp )
stamp = 0;
end
stamp = stamp + uint32(1);
result = stamp;
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能,这是设计使然。您应该使用
持久
变量(1980 年的 MATLAB 技术已应用于 2011 年)!为了完整起见,我应该提到,实际上从 2010b 开始,就有一个未记录且可能不再受支持的
static
属性修饰符。有关背景信息,请参阅此处答案Dave Foti,MATLAB OO 小组经理:
You can not, it is by design. You should use a
persistent
variable (technique from the MATLAB as 1980 applied in year 2011)!For completeness I should mention that actually there is as of 2010b an undocumented and probably not longer supported
static
property modifier.For background see here the answer of Dave Foti, MATLAB OO group manager:
这是在 Matlab 中创建静态属性的直接方法。此实现与假设的(但不可能;请参阅 Mikhail 的答案)真正的静态属性之间的唯一区别是设置成员变量的语法。
现在,静态属性 staticVar 可以通过以下方式读取:
...并通过以下方式设置:
因此,例如,这是此功能测试的预期输出:
这种方法对于您请求的私有静态属性也同样适用,但是演示代码有点长。请注意,这不是一个句柄类(尽管它在句柄类上也可以很好地工作)。
...以及测试:
Here's a direct way to create a static property in Matlab. The only difference between this implementation and a hypothetical (but impossible; see Mikhail's answer) true static property is the syntax for setting the member variable.
Now the static property staticVar can be read via:
...and be set via:
So, for instance, this is the expected output from a test of this functionality:
This approach works just as well for private static properties like you requested, but the demo code is a little longer. Note that this is not a handle class (though it would work perfectly well on a handle class as well).
...and the test:
(只是为了通知)
有(另一种?)方法可以在 matlab 中创建类似静态的数据
假设您有一个“handle”类,其名称为“car”
如果您希望汽车类具有静态数据,您可以构造另一个句柄类并在汽车类抛出组合中使用它,后一个类作为汽车类的静态数据,
这样当您创建汽车类的第一个实例时,将创建 STATIC_DATA_HOLDER 的实例,当您创建汽车类的第二个实例时,它将使用之前创建的 STATIC_DATA_HOLDER 类。
这些代码使用“MATLAB 2013b”进行了测试
(just to inform)
there is (another?) way to create static-like data in matlab
suppose that you have a "handle" class which its name is "car"
if you want the car class to have static data, you could construct another handle class and use it in car class throw composition, the latter class works as a static data for car class
this way when you create first instance of a car class, an instance of STATIC_DATA_HOLDER will be created and when you create second instance of car class it uses previously created STATIC_DATA_HOLDER class.
these code tested with "MATLAB 2013b"
获取静态属性之类的另一种解决方法是利用成员变量的初始化代码仅在加载类文件时执行一次这一事实。这意味着,如果您有一个类似
some_function
的定义,则仅调用一次,并且如果它返回类类型的对象,则这将由所有实例共享。我添加了一个示例实现来展示如何使用它:如果您运行此示例代码
,您将看到
classvars
确实是共享的。我认为这个解决方案比在函数中使用持久变量要好得多,因为您可以根据需要多次重用StaticVarContainer
,它更容易使用,而且您可以直接看到静态变量的初始化在属性部分。为了获得OP问题中所期望的结果(即实现对象计数器),可以将共享属性设置为常量,以便可以在手头没有实例的情况下引用它:
注意,
Constant
属性仅意味着,例如obj1.static
无法更改,但它不会影响不恒定的obj1.static.counter
,并且可以根据自己的意愿进行设置。Another workaround to get something like static properties is to use the fact that initialisation code for member variables is only executed once when the class file is loaded. That means, if you have a definition like
then
some_function
is invoked only once, and if it returns an object of class type, this will be shared by all instances. I've added a sample implementation that shows how that can be used:If you run this sample code
you'll see, that
classvars
is indeed shared. I think this solution is much nicer than using persistent variables in functions, since you can reuse theStaticVarContainer
as often as you want, it's easier to use, and furthermore, you directly see the initialisation of the static variables in the properties section.To get the result, that is desired in the OP's question (i.e. implementing an object counter) the shared property can be made
Constant
, so that it can be referenced without an instance at hand:Note, that the
Constant
attribute only means that, e.g.obj1.static
cannot be changed, but it does not affectobj1.static.counter
which is not constant, and can be set to heart's desire.