MATLAB Object-Oriented Programming

(Joyce) #1

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.

10 Object Arrays

Free download pdf