Accessing Dynamic Properties in Arrays
You cannot reference all the dynamic properties in an object array using a single statement, as you can with ordinary properties. For example, the ObjectArrayDynamic
class subclasses the dynamicprops
class.
classdef ObjectArrayDynamic < dynamicprops properties RegProp end methods function obj = ObjectArrayDynamic obj.RegProp = randi(100); end end end
You can add dynamic properties to objects of the ObjectArrayDynamic
class. Create an object array and add dynamic properties to each member of the array. Define elements 1 and 2 as ObjectArrayDynamic
objects:
a(1) = ObjectArrayDynamic; a(2) = ObjectArrayDynamic;
Add dynamic properties to each object and assign a value.
a(1).addprop('DynoProp'); a(1).DynoProp = 1; a(2).addprop('DynoProp'); a(2).DynoProp = 2;
Get the values of the ordinary properties, as with any array.
a.RegProp
ans = 4 ans = 85
However, MATLAB® returns an error if you try to access the dynamic properties of all array elements using this syntax.
a.DynoProp
No appropriate method, property, or field 'DynoProp' for class 'ObjectArrayDynamic'.
Refer to each object individually to access dynamic property values:
a(1).DynoProp
ans = 1
a(2).DynoProp
ans = 2
For information about classes that can define dynamic properties, see Dynamic Properties — Adding Properties to an Instance .