empty
Creates an empty array of a given class.
Category: Programming > Classes
Syntax
E = ClassName.empty
Returns a 0-by-0 empty array of the class.
E = ClassName.empty(m, n, ...)
Returns an empty array with the given size, in which at least one dimension is 0.
Description
empty makes it explicit what type of data an empty variable will hold, and gives it a specific shape. That shape matters: a 0-by-3 empty matrix is the right starting point for building a table of three-column rows with vertical concatenation, while [] is 0-by-0.
It also creates empty arrays of objects, as in MyClass.empty, which can later grow by indexing.
Input arguments
ClassName- A class name such as
double,char,single,int8,cellorstruct, or a user-defined class. m, n, ...- Dimension sizes. At least one must be
0, otherwise it is an error.
Output arguments
E- Empty array of the requested class and size.
Example
Typed empty arrays
x = double.empty(0, 3);
size(x)
rows = [x; 1 2 3; 4 5 6]
class(int8.empty(2, 0))
size(cell.empty)
Output
ans = 1×2
0 3
rows = 2×3
1 2 3
4 5 6
ans = 'int8'
ans = 1×2
0 0
Notes
empty works for the built-in classes. For a user-defined class, MyClass.empty is not yet recognized.