Main Content

empty

Create empty array of specified class

Description

This page describes theemptymethod, which creates empty arrays of a given class. To test if an existing array is an empty array, useisempty.

example

A= ClassName.empty返回一个n empty 0-by-0 array of the class named byClassName. ReplaceClassNamewith the actual name of the class.

example

A= ClassName.empty(sz1,...,szN)返回一个n empty array with the specified dimensions. At least one of the dimensions must be 0.

example

A= ClassName.empty(sizeVector)返回一个n 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 thesizefunction 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 emptyint16array with nonzero dimensions. Specify the 5-by-0 dimensions as inputs to theemptymethod.

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

使用现有的e的大小mpty 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 be0. Negative values are treated as0. Trailing dimensions of1are 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 be0. Negative values are treated as0.

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

Theemptymethod 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 classdouble:

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 theRGBColorclass defined here.

classdefColorInRGBpropertiesColor(1,3)= [1,0,0];endmethodsfunctionobj = ColorInRGB(c)ifnargin > 0 obj.Color = c;endendendend

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

A = ColorInRGB.empty(0,5);

Identify Empty Arrays

You can use theisempty,size, andlengthfunctions to identify empty object arrays. For example, create an empty array of theColorInRGBclass 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 theColorInRGBclass 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

  • emptyis a hidden, public, static method of all nonabstract MATLAB®classes. You can override theemptymethod 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

||

Topics