Main Content

Accessing Properties and Methods in Object Arrays

You can access properties and invoke methods of object arrays as a whole or work with individual elements of the array. The examples in this topic use a modified version of BasicClass from Creating a Simple Class:

classdef BasicClass
   properties
      prop1 {mustBeNumeric} = 0
   end
   methods
      function r = roundOff(obj)
         r = round([obj.prop1],2);
      end
      function r = multiplyBy(obj,n)
         r = [obj.prop1]*n;
      end
   end
end

Create a BasicClass object array with a loop. The loop assigns the value of i to prop1 of each array element a(i). For more information, see Create and Initialize Object Arrays.

for i = 1:4
a(i) = BasicClass;
a(i).prop1 = i;
end

Reference Properties in an Object Array

You can retrieve all values of the same property in an object array using dot notation. This command returns a comma-separated list of all the property values.

a.prop1
ans =

     1


ans =

     2


ans =

     3


ans =

     4

To collect the comma-separated list in a vector and assign it to a variable, enclose the object array expression in brackets.

prop1Values = [a.prop1]
prop1Values =

     1     2     3     4

Note

Dynamic properties are defined per object and are therefore not properties of the class. Because of this distinction, even if every element of the array defines the same dynamic property, MATLAB® returns an error if you try to access the dynamic properties of an entire array in a single statement. Index into each object individually to access dynamic property values. For information about classes that can define dynamic properties, see Dynamic Properties — Adding Properties to an Instance.

You can reference the property values of individual elements in an object array using standard MATLAB indexing syntax. For example, return the value of prop1 for the object a(2).

a(2).prop1
ans =

     2

You can also return a subset of prop1 values from the object array.

prop1partialValues = [a(2:3).prop1]
prop1partialValues =

     2     3

Assigning Values to Properties in an Object Array

You cannot assign new values to prop1 of all elements in an object array with a simple assignment statement, but you can use a loop to assign values to all properties in one step. Assign new values to prop1 of all four elements in the array and verify the results.

newValues = [1.4142 1.6180 2.7183 3.1415];
for i = 1:4
a(i).prop1 = newValues(i);
end
propValues = [a.prop1]
propValues =

    1.4142    1.6180    2.7183    3.1415

Assign property values of individual array elements using standard indexing syntax. For example, assign a new value to a(2).prop1 and verify the result.

a(2).prop1 = 1.7321;
a(2).prop1
ans =

    1.7321

Invoking Methods on an Object Array

When you pass an object array to a method, MATLAB invokes the method on the entire array. Call the roundOff method of BasicClass on the object array a from the previous section.

roundOff(a)
ans =

    1.4100    1.7300    2.7200    3.1400
Class methods must be designed to handle the size and shape of the object arrays you need to create. In BasicClass, the roundOff and multiplyBy methods are designed to handle arrays of any size by enclosing the property references in square brackets.
r = round([obj.prop1],2);
r = [obj.prop1]*n;
A method that only works on scalar instances errors if passed a nonscalar array. For more information, see Vectorize Methods.

You can also use standard function notation to invoke a method for one element of the object array. For example, call the roundOff method on a(3).

roundOff(a(3))
ans =

    2.7200

Use a similar syntax for a method that takes one or more input arguments.

multiplyBy(a(3),5)
ans =

   13.5915

Related Topics