This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Empty Arrays

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
      function obj = SimpleValue(v)
         if nargin > 0
            obj.Value = v;

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

ary = SimpleValue.empty(5,0)
ary = 

  5x0 SimpleValue array with properties:


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 every other array element. Once you assign a nonempty object to an array, all array elements must be nonempty objects.

    Note:   A class constructor must avoid returning empty objects by default.

For example, using the SimpleValue defined in the Initialize Object Arrays section, create an empty array:

ary = SimpleValue.empty(5,0);
ans =


ary is an array of class SimpleValue. However, it is an empty array:

Index exceeds matrix dimensions.

If you make an assignment to a property value, MATLAB calls the SimpleClass constructor to grow the array to the require size:

ary(5).Value = 7;
ans = 

ans = 


MATLAB populates array elements one through five with SimpleValue objects created by calling the class constructor with no arguments. Then MATLAB assigns the property value 7 to the object at ary(5).

Related Topics

Was this topic helpful?