cell

Creates a cell array filled with empty arrays.

Category: Language Fundamentals > Data Types

Syntax

C = cell(n)

Creates an n-by-n cell array.

C = cell(m, n, ...)

Creates a cell array with the given sizes.

C = cell(sz)

Takes the sizes from the vector sz.

Description

A cell array holds values of any type, one per cell. cell is mainly used to preallocate, which avoids the array growing inside a loop: c = cell(1, 5); c{k} = ....

Contents are read with braces, c{2}, and the cell itself with parentheses, c(2). cell(0) gives an empty cell array.

Input arguments

n, m, ...
Nonnegative integer dimensions.
sz
Vector of dimensions.

Output arguments

C
Cell array whose elements are all [].

Example

Preallocate and fill

c = cell(1, 3);
for k = 1:3
  c{k} = k^2;
end
c
Output
c = 1×3 cell

    {[1]}    {[4]}    {[9]}

See also

cellfun cell2mat iscell struct repmat