Main Content

Create and Initialize Object Arrays

You can create arrays of class-based objects with indexing behavior similar to built-in types like double or string. However, creating object arrays requires particular attention to how the objects are constructed.

Create an Object Array Using a Loop

Define the class SimpleValue, which has one property with an initial value and the default, no-argument constructor.

classdef SimpleValue
   properties
      prop1 = 0
   end
end

Create an array of SimpleValue objects using a loop.

for k = 1:5
   objArray(k) = SimpleValue;
end

Reference and assign the property values of specific objects using array indexing. For example, assign the value of 5 to the prop1 property of objArray(1).

objArray(1).prop1 = 5;

Return the values of prop1 for all the objects and assign them to a vector.

p = [objArray.prop1]
p =

     5     0     0     0     0

For more information on accessing properties and methods of an object array, see Accessing Properties and Methods in Object Arrays.

Create an Object Array by Constructing the Last Element

In the SimpleValue example, the objects in objArray are created inside a loop using the default, no-argument constructor and initial property values. You can also create an array of SimpleValue objects by constructing the last element of the array.

For example, create a 2-by-2 array of SimpleValue objects.

a(2,2) = SimpleValue
a = 

  2×2 SimpleValue array with properties:

    prop1

When you create an object array this way, MATLAB® takes one of two approaches to fill in the rest of the elements of the array, depending on whether the class is value or handle.

Value Class Arrays

After you construct the last element in an array of value class objects, MATLAB:

  • Calls the no-argument constructor once

  • Fills in the rest of the elements with copies of that instance

If you want to initialize property values using an input to the constructor, you must also have a no-argument option in the constructor.

For example, add a constructor to SimpleValue that accepts an input and assigns it that input to prop1.

classdef SimpleValue
   properties
      prop1 = 0
   end
   methods
      function obj = SimpleValue(v)
         if nargin > 0
            obj.prop1 = v;
         end
      end
   end
end

Create a 1-by-5 array by constructing b(5) with input argument 7.

b(5) = SimpleValue(7);

Return all of the prop1 values and assign them to a vector y. The object in b(5) has a prop1 value of 7. MATLAB calls the no-argument constructor once and copies that value to all the remaining elements in the array. Those elements have the initial prop1 value of 0.

y = [b.prop1]
y =

     0     0     0     0     7

If the SimpleValue constructor cannot handle no-argument calls, MATLAB errors when trying to populate b(1) through b(4).

Handle Class Arrays

After you construct the last element in an array of handle class objects, MATLAB:

  • Creates unique handles for each element of the array

  • Copies the property values from the last object in the array

  • Creates objects for the rest of the array using those copied values, without calling the constructor

Unlike the case for value classes, MATLAB does not call the constructor to populate the array, so all objects in that array start with the same property values. For example, define a handle class that generates random numbers for its property.

classdef InitHandleArray < handle
   properties
      RandNum
   end
   methods
      function obj = InitHandleArray
         obj.RandNum = randi(100);
      end
   end
end

Create a new 1-by-5 array by constructing c(5), and verify the prop1 values. To create the array, MATLAB calls the constructor twice. The first time, MATLAB assigns a value to c(5).RandNum. The second time, it assigns the result of a second call to randi to create c(1). Finally, it copies this value of RandNum to the rest of the elements in c.

c(5) = InitHandleArray;
z = [c.RandNum]
z =

    91    91    91    91    82

Although MATLAB does not need a no-argument option in the constructor to create a handle class array, having a no-argument option is still the recommended practice for all class constructors.

Create an Object Array in the Constructor

You can also use the class constructor itself to create and return an object array. For example, the ObjectArray class creates an object array that is the same size as the 2-D input array F. The constructor then initializes the Value property of each object to the corresponding input array value.

classdef ObjectArray
   properties
      Value
   end
   methods
      function obj = ObjectArray(F)
         if nargin ~= 0
            m = size(F,1);
            n = size(F,2);
            p(m,n) = obj;
            for i = 1:m
               for j = 1:n
                  p(i,j).Value = F(i,j);
               end
            end
            obj = p;
         end
      end
   end
end

This pattern for the constructor works for both value and handle objects as long as the no-argument option is included.

Related Topics