Dynamically add a field to an object in matlab
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')
精彩评论