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 dynamic dynamic. I want

 myfoo.newfield = 42; 

but it will cause an error.

I know that there is a way to dynamically add a field / property to a MATLAB object, but I cannot remember it or easily find it in the help. Does anyone know the syntax?

+4
source share
1 answer

Ok, found it. But this is not general, only subclasses of the dynamicprops class implement it. This is what I remember. Therefore, I suspect that the general answer to this question is that you cannot do this.

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

 P = addprop(H,'PropertyName') 
+6
source

Source: https://habr.com/ru/post/1312224/


All Articles