Create and Initialize Object Arrays
MATLAB® provides different ways of creating object arrays. These different methods vary in how the objects in the array are created. Choose a method of array creation based on your end goal:
Create an Object Array Using a Loop — Use a for-loop to create arrays when you need to initialize objects with different values.
Create an Object Array Using createArray (since R2024a) — Use
createArray
(since R2024a) to create an array of objects with the same initial values.Create an Object Array by Constructing the Last Element — Use when you want to construct only one element of an array. MATLAB fills the rest of the array with copies of the default object of the class of the last element.
Create an Object Array Using a Loop
Define the class SimpleValue
, which has one property with a default
value and a constructor that can be called with no arguments.
classdef SimpleValue properties prop1 = 0 end methods function obj = SimpleValue(v) if nargin > 0 obj.prop1 = v; end end end end
Create an array of SimpleValue
objects using a loop. Set the value of
prop1
of each element of the array to a different value.
initValues = [3 -1 0 4 5]; for k = 1:5 objArray(k) = SimpleValue(initValues(k)); end
Verify that the values of prop1
are set to
initValues
.
p = [objArray.prop1]
p = 3 -1 0 4 5
For more information on accessing properties and methods of an object array, see Accessing Properties and Methods in Object Arrays.
Create an Object Array Using createArray
(since R2024a)
You can also use createArray
to generate object arrays of your
class. createArray
enables you to specify the size of the array, as
well as combinations of class, prototype, and fill value to generate arrays of objects.
For the full descriptions of syntaxes and options, see createArray
.
classname
Argument
Use the classname
argument to specify the class of the
array:
X = createArray(dims,"classname")
Create a 1-by-5 array of the SimpleValue
class from Create an Object Array Using a Loop. MATLAB calls the constructor once with no arguments and populates the array
with copies of that instance.
X = createArray(1,5,"SimpleValue")
X = 1×5 SimpleValue array with properties: prop1
The value of prop1
in all elements of the array is the
default value defined by the class.
[X.prop1]
ans = 0 0 0 0 0
Like
Argument
Use the Like
name-value argument to create an object array
with an existing instance or array as a prototype:
X = createArray(dims,Like=prototype)
Create an instance phi
of SimpleValue
with
1.618
assigned to prop1
. Use
Like
with phi
to create a 2-by-2 object array
of the class. The returned array L
has the same class as
phi
.
phi = SimpleValue(1.618); L = createArray(2,2,Like=phi)
L = 2×2 SimpleValue array with properties: prop1
createArray
used with Like
does not
preserve the property value of phi
because it does not use copies
of the prototype to fill the array. The elements in L
are copies
of one call to the default constructor of the class of the prototype, so they have
the default value for prop1
.
[L.prop1]
ans = 0 0 0 0
FillValue
Argument
To create an array with objects other than the default, create an instance using
input arguments to the constructor and then use that as a fill value for
createArray
:
X = createArray(dims,FillValue=instance)
Create an instance of SimpleValue
with 7
assigned to prop1
, and use that instance with the
FillValue
name-value argument to create a 3-by-1 array.
s = SimpleValue(7); F = createArray(3,1,FillValue=s)
F = 3×1 SimpleValue array with properties: prop1
classname
, createArray
does not call the
class constructor when you specify a fill value. The fill value is copied to each
element of the array. Verify that the value of prop1
of each
instance is 7
.[F.prop1]
ans = 7 7 7
Handle Classes
The previous sections focused on value classes. For handle classes,
createArray
creates unique handles for each element of the
array. For example, define a handle class with a constructor that generates a random
number for a class property.
classdef InitHandleArray < handle properties Num end methods function obj = InitHandleArray(varargin) if nargin == 1 obj.Num = randi(varargin{1}); else obj.Num = -4; end end end end
Use createArray
to return a 2-by-2 array of the default
InitHandleArray
object.
Y = createArray(2,2,"InitHandleArray")
Y = 2×2 InitHandleArray array with properties: Num
Although the handles are unique, MATLAB calls the constructor only once and populates the array with copies of
that instance. Verify that the no-argument constructor was called by checking the
values of property Num
.
[Y.Num]
ans = -4 -4 -4 -4
Create an Object Array by Constructing the Last Element
In the SimpleValue
example in Create an Object Array Using a Loop,
the objects in objArray
are created inside a loop. 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 by
constructing the last element in the array.
a(2,2) = SimpleValue
a = 2×2 SimpleValue array with properties: prop1
Value Class Arrays
After you construct the last element in an array of value class objects, MATLAB:
Calls the no-argument constructor
Fills in the rest of the elements with copies of that instance
Create a 1-by-5 array of SimpleValue
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 default
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
Calls the no-argument constructor
Fills in the rest of the elements with copies of that instance
Create a new 1-by-5 array of the InitHandleArray
class (see Handle Classes) by constructing
c(5)
with an input argument of 3
, and verify
the Num
values. MATLAB calls the constructor again and assigns copies of the result of that
call to c(1)
through c(4)
.
c(5) = InitHandleArray(3); z = [c.Num]
z = -4 -4 -4 -4 3
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 and shape as the 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 n = numel(F); p(n) = obj; for i = 1:n p(i) = F(i); end obj = reshape(p,size(F)); 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.