开发者

Dynamically add a field to an object in matlab

开发者 https://www.devze.com 2023-01-02 13:07 出处:网络
Say I have a MATLAB object defined in a class file classdef foo properties bar end end And I create a foo object开发者_开发问答

Say I have a MATLAB object defined in a class file

classdef foo

   properties
      bar
   end

end

And I create a foo object

开发者_开发问答
myfoo = foo();

Now I want to add another field to foo dynamically. What I want is

myfoo.newfield = 42;

but this will throw an error.

I know there is a way to dynamically add a field/property to a MATLAB object but I can't remember it or find it easily in the help. Anyone know the syntax?


Ok, found it. But it's not general, only subclasses of the dynamicprops class implement it. This is what I remember coming across. So I suspect the general answer to this question is you can't do it.

Any class that is a subclass of the dynamicprops class (which is itself a subclass of the handle class) can define dynamic properties using the addprop method. The syntax is:

P = addprop(H,'PropertyName')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号