Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add a field to an object in matlab

Tags:

oop

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?

like image 980
Marc Avatar asked Jun 08 '10 15:06

Marc


1 Answers

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')
like image 69
Marc Avatar answered Oct 01 '22 11:10

Marc