During the creation of object arrays, MATLAB® can call the class constructor with no arguments, even if the constructor does not build an object array. For example, suppose that you define the following class:
classdef SimpleValue properties Value end methods function obj = SimpleValue(v) obj.Value = v; end end end
Execute the following statement to create an array:
a(1,7) = SimpleValue(7)
Error using SimpleValue (line 7)
Not enough input arguments.
This error occurs because MATLAB calls the constructor
with no arguments to initialize elements 1
through 6
in
the array.
Your class must support the no input argument constructor syntax.
A simple solution is to test nargin
and
let the case when nargin == 0
execute no code,
but not error:
classdef SimpleValue properties Value end methods function obj = SimpleValue(v) if nargin > 0 obj.Value = v; end end end end
Using the revised class definition, the previous array assignment statement executes without error:
a(1,7) = SimpleValue(7)
a = 1x7 SimpleValue array with properties: Value
The object assigned to array element a(1,7)
uses
the input argument passed to the constructor as the value assigned
to the property:
a(1,7)
ans = SimpleValue with properties: Value: 7
MATLAB created the objects contained in elements a(1,1:6)
with
no input argument. The default value for properties empty []
.
For example:
a(1,1)
ans = SimpleValue with properties: Value: []
MATLAB calls the SimpleValue
constructor
once and copies the returned object to each element of the array.
When MATLAB calls a constructor with no arguments to initialize an object array, one of the following assignments occurs:
If property definitions specify default values, MATLAB assigns these values.
If the constructor assigns values in the absence of input arguments, MATLAB assigns these values.
If neither of the preceding situations apply, MATLAB assigns
the value of empty double (that is, []
) to the
property.