Documentation Center

  • Trial Software
  • Product Updates

empty

Create empty array

Syntax

A = ClassName.empty
A = ClassName.empty(n,m,p,...)
A = ClassName.empty([n,m,p,...])

Description

Use empty to create empty arrays of the specified class, ClassName. Specify at least one dimension of the array as 0. MATLAB® treats negative values as 0.

A = ClassName.empty returns an empty 0-by-0 array of the class of ClassName.

A = ClassName.empty(n,m,p,...) returns an empty rectangular array with the specified dimensions. At least one of the dimensions must be 0.

A = ClassName.empty([n,m,p,...]) returns an empty rectangular array with the specified dimensions. At least one of the dimensions must be 0. This syntax is useful when using the values returned by the size function to define an empty array that is the same size as an existing empty array:

A = ClassName.empty(size(otherEmptyArray));

Input Arguments

n,m,p,...

Dimensions of the empty array. At least one of the specified dimensions must be 0.

Output Arguments

A

An empty array of the specified dimensions and of the class used in the method invocation.

Attributes

empty is a hidden, public, static method of all nonabstract MATLAB classes.

AccessPublic
Hiddentrue
Statictrue

To learn about attributes of methods, see Method Attributes in the MATLAB Object-Oriented Programming documentation.

Examples

Use empty to create a rectangular empty array of class int16:

A = int16.empty(5,0);
whos
  Name      Size            Bytes  Class    Attributes

  A         5x0                 0  int16

Using the empty method of the int16 class to produce an empty array in which some dimensions are not zero is simpler than using conversion and reshape operations:

A = int16([]);
A = reshape(A,5,0);
whos
  Name      Size            Bytes  Class    Attributes

  A         5x0                 0  int16

Given the following definition for a class,

classdef ExEmpty
   properties
      Color = [1,0,0];
   end
   methods
      function obj = ExEmpty(c)
         if nargin > 0
            obj.Color = c;
         end
      end
   end
end

Create an empty array of class ExEmpty:

A = ExEmpty.empty;
 whos
  Name        Size            Bytes  Class     Attributes

  A           0x0             104  ExEmpty

One dimension of an empty array must be zero:

A5 = ExEmpty.empty(0,5);
 whos
  Name        Size            Bytes  Class     Attributes

  A5           0x5             104  ExEmpty

Empty object arrays follow array concatenation behavior:

B = [A,A5]
B = 

  0x5 ExEmpty array with properties:

    Color

You cannot index into an empty array:

A5(1)
Index exceeds matrix dimensions.

You can use the isempty, size, and length functions to identify empty object arrays:

isempty(A5)

ans =

     1
size(A5)

ans =

     0     5
length(A5)

ans =

     0

Class of Empty Object Array

The empty method enables you to initialize arrays of a specific class:

C = char.empty(0,7)

C =

   Empty string: 0-by-7

class(C)
ans =

char

Initializing an array with empty brackets ([]):

a = [];

produces an array of class double:

class(a)
ans =

double

See Also

| |

Tutorials

Was this topic helpful?