cell - Cell array - MATLAB (original) (raw)

Description

A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of text, combinations of text and numbers, or numeric arrays of different sizes. Refer to sets of cells by enclosing indices in smooth parentheses, (). Access the contents of cells by indexing with curly braces, {}.

Creation

When you have data to put into a cell array, create the array using the cell array construction operator, {}.

C = {1,2,3; 'text',rand(5,10,2),{11; 22; 33}}

C=2×3 cell array {[ 1]} {[ 2]} {[ 3]} {'text'} {5×10×2 double} {3×1 cell}

You also can use {} to create an empty 0-by-0 cell array.

To create a cell array with a specified size, use the cell function, described below.

You can use cell to preallocate a cell array to which you assign data later. cell also converts certain types of Java®, .NET, and Python® data structures to cell arrays of equivalent MATLAB® objects.

Syntax

Description

[C](#d126e197240) = cell([n](#d126e196997)) returns an n-by-n cell array of empty matrices.

example

[C](#d126e197240) = cell([sz1,...,szN](#d126e197056)) returns a sz1-by-...-by-szN cell array of empty matrices where sz1,...,szN indicate the size of each dimension. For example, cell(2,3) returns a 2-by-3 cell array.

example

[C](#d126e197240) = cell([sz](#d126e197119)) returns a cell array of empty matrices where size vectorsz defines size(C). For example,cell([2 3]) returns a 2-by-3 cell array.

example

[D](#d126e197260) = cell([obj](#d126e197187)) converts a Java array, .NET System.String orSystem.Object array, or Python sequence into a MATLAB cell array.

Input Arguments

expand all

Size of a square cell array, specified as an integer value.

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

Sizes of the dimensions of the cell array, specified as integer values.

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

Size, specified as a row vector of integers. Each element ofsz indicates the size of the corresponding dimension.

Example: sz = [2 3 4] creates a 2-by-3-by-4 cell array of empty matrices.

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

Input array, specified as:

Output Arguments

expand all

Output array, returned as a cell array. Each cell contains an empty, 0-by-0 array of type double.

Examples

collapse all

When related pieces of data have different data types, you can keep them together in a cell array. Each cell contains a piece of data. To refer to elements of a cell array, use array indexing. You can index into a cell array using smooth parentheses, (), and into the contents of cells using curly braces, {}.

Create a cell array that contains several temperature readings taken on a given date. Specify a date as a character vector, and temperatures as an array of doubles. To store these pieces of data in a cell array, enclose them in curly braces.

C = {'2017-08-16',[56 67 78]}

C=1×2 cell array {'2017-08-16'} {[56 67 78]}

Add readings for different dates to the cell array. One way to add more cells is to expand the cell array by assignment, just as you can expand an ordinary array.

C(2,:) = {'2017-08-17',[58 69 79]}; C(3,:) = {'2017-08-18',[60 68 81]}

C=3×2 cell array {'2017-08-16'} {[56 67 78]} {'2017-08-17'} {[58 69 79]} {'2017-08-18'} {[60 68 81]}

Index into the first row of C. When you index with smooth parentheses, (), the result is a cell array that is a subset of the cell array.

ans=1×2 cell array {'2017-08-16'} {[56 67 78]}

Index into the contents of a cell. When you index with curly braces, {}, the result is the piece of data that is contained in the specified cell.

Create a 3-by-3 cell array of empty matrices.

C=3×3 cell array {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}

Create a 3-by-4-by-2 cell array of empty matrices.

Create a cell array of empty matrices that is the same size as an existing array.

A = [7 9; 2 1; 8 3]; sz = size(A); C = cell(sz)

C=3×2 cell array {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}

It is a common pattern to combine the previous two lines of code into a single line.

Tips

Extended Capabilities

Version History

Introduced before R2006a

expand all

Between R2021b and R2023b, MATLAB displayed the entire contents of an array in a cell if the contents fit in the display. If the array did not fit, MATLAB showed as much of the array as the display allowed.

Starting in R2023b, if the entire contents of an array in a cell do not fit in the display, MATLAB shows as much of the array as the display allows, as well as the size and data type of the array. For example, as of R2023b, MATLAB displays partial contents as well as size and data type for a cell that contains an array of 100 double values.

ans =

1×1 cell array

{[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 … ] (1×100 double)}

If the first element of the array is itself too large to display, such as an enumeration member with a long name, MATLAB displays only the size and data type of the array.

The disp function follows the same behavior.

Before R2021b, MATLAB displayed the entire contents of an array in a cell if the contents fit in the display. If the array did not fit, MATLAB displayed the size and data type of the array.

Starting in R2021b, if the entire contents of an array do not fit in the display, MATLAB shows as much of the array as space allows instead of the size and data type. For example, as of R2021b, MATLAB displays partial contents of a cell that contains an array of 100 double values.

ans =

1×1 cell array

{[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 … ]}

If the first element of the array is itself too large to display, such as an enumeration member with a long name, MATLAB displays only the size and data type.

The disp function follows the same behavior.

Starting in R2019a, the dimensions of an expanded cell array are consistent whether you use curly braces or parentheses for indices. Previously, the output dimensions were different when you did not specify indices for all dimensions. Indexing with curly braces now matches the previous behavior for indexing with parentheses, which is consistent with general array expansion. For example: