MATLAB Object-Oriented Programming

(Joyce) #1

Empty Arrays


In this section...
“Creating Empty Arrays” on page 10-8
“Assigning Values to an Empty Array” on page 10-8

Creating Empty Arrays


Empty arrays have no elements, but are of a certain class. All nonabstract classes have a
static method named empty that creates an empty array of the same class. The empty
method enables you to specify the dimensions of the output array. However, at least one
of the dimensions must be 0. For example, define the SimpleValue class:

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

Create a 5–by–0 empty array of class SimpleValue.

ary = SimpleValue.empty(5,0)

ary =

5x0 SimpleValue array with properties:

Value

Calling empty with no arguments returns a 0–by–0 empty array.

Assigning Values to an Empty Array


An empty object defines the class of an array. To assign nonempty objects to an empty
array, MATLAB calls the class constructor to create default instances of the class for

10 Object Arrays

Free download pdf