Main Content

empty

Create empty array of specified class

Description

This page describes the empty method, which creates empty arrays of a given class. To test if an existing array is an empty array, use isempty.

example

A = ClassName.empty returns an empty 0-by-0 array of the class named by ClassName. Replace ClassName with the actual name of the class.

example

A = ClassName.empty(sz1,...,szN) returns an empty array with the specified dimensions. At least one of the dimensions must be 0.

example

A = ClassName.empty(sizeVector) returns an empty array with the specified dimensions. At least one of the dimensions must be 0. Use this syntax to define an empty array that is the same size as an existing empty array. Pass the values returned by the size function as inputs.

Examples

collapse all

This example shows how to create an empty character array using the default dimensions, 0-by-0.

A = char.empty
A =

  0x0 empty char array

This example shows how to create an empty int16 array with nonzero dimensions. Specify the 5-by-0 dimensions as inputs to the empty method.

Aint = int16.empty(5,0)
Aint =

  5x0 empty int16 matrix

Use the size of an existing empty array to create an array of the same size.

Aint = int16.empty(5,0);
Bdouble = double.empty(size(Aint))
Bdouble =

  5x0 empty double matrix

Input Arguments

collapse all

Dimensions of array, specified as integers. At least one dimension must be 0. Negative values are treated as 0. Trailing dimensions of 1 are not included in the size of the array

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Vector of dimensions, specified as a row vector of nonnegative integers. At least one element must be 0. Negative values are treated as 0.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Output Arguments

collapse all

Empty array, returned as an empty array of the specified dimensions and of the class used in the method invocation.

More About

collapse all

Class of Empty Object Array

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

C = char.empty(0,7)

C =

   Empty matrix: 0-by-7

disp(class(C))
char

Initializing an array with empty brackets ([]) produces an empty array of class double:

a = [];
disp(class(a))
double

You can initialize an empty array of a user-defined class. For example, the empty static method is a hidden method of the RGBColor class defined here.

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

To create an empty 0-by-5 array of class ColorInRGB, call the empty method:

A = ColorInRGB.empty(0,5);

Identify Empty Arrays

You can use the isempty, size, and length functions to identify empty object arrays. For example, create an empty array of the ColorInRGB class defined in the previous section.

A = ColorInRGB.empty(0,5);
isempty(A)
ans =

  logical

   1
size(A)
ans =

     0     5
length(A)
ans =

     0

Concatenation and Indexing of Empty Arrays

Empty arrays follow array concatenation behavior. For example, create an empty array of the ColorInRGB class defined in the previous section and for a new array by concatenating instances into another array.

A = ColorInRGB.empty(0,5);
B = [A A]
B = 

  0×10 ColorInRGB array with properties:

    Color

You cannot index into an empty array.

B(0,3)
Index in position 1 is invalid. Array indices must be positive integers or logical values.

Tips

  • empty is a hidden, public, static method of all nonabstract MATLAB® classes. You can override the empty method in class definitions.

  • This method is useful for creating empty arrays of data types that do not have a special syntax for creating empty arrays, such as [] for double arrays.

Version History

Introduced in R2008a

See Also

| |