Main Content

cell

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'}    {5x10x2 double}    {3x1 cell}

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

C = {}
C =

  0x0 empty 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.

Description

example

C = cell(n) returns an n-by-n cell array of empty matrices.

example

C = cell(sz1,...,szN) 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 = cell(sz) returns a cell array of empty matrices where size vector sz defines size(C). For example, cell([2 3]) returns a 2-by-3 cell array.

D = cell(obj) converts a Java array, .NET System.String or System.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.

  • If n is 0, then C is an empty cell array.

  • If n is negative, then it is treated as 0.

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

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

  • If the size of any dimension is 0, then C is an empty cell array.

  • If the size of any dimension is negative, then it is treated as 0.

  • Beyond the second dimension, cell ignores trailing dimensions with a size of 1. For example, cell(3,1,1,1) produces a 3-by-1 cell array of empty matrices.

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

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

  • If the size of any dimension is 0, then C is an empty cell array.

  • If the size of any dimension is negative, then it is treated as 0.

  • Beyond the second dimension, cell ignores trailing dimensions with a size of 1. For example, cell([3 1 1 1]) produces a 3-by-1 cell array of empty matrices.

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:

  • Java array or object

  • .NET array of type System.String or System.Object

  • Python sequence type

Output Arguments

expand all

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

Converted array, returned as a cell array.

Each cell contains a MATLAB object that has a type closest to the corresponding Java, .NET, or Python type. For more information, see:

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.

C(1,:)
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.

C{1,2}
ans = 1×3

    56    67    78

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

C = cell(3)
C=3×3 cell array
    {0x0 double}    {0x0 double}    {0x0 double}
    {0x0 double}    {0x0 double}    {0x0 double}
    {0x0 double}    {0x0 double}    {0x0 double}

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

C = cell(3,4,2);
size(C)
ans = 1×3

     3     4     2

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
    {0x0 double}    {0x0 double}
    {0x0 double}    {0x0 double}
    {0x0 double}    {0x0 double}

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

C = cell(size(A));

Tips

  • Creating a cell array of empty matrices with the cell function is equivalent to assigning an empty matrix to the last index of a new cell array. For example, these two statements are equivalent:

    C = cell(3,4,2);
    C{3,4,2} = [];

Extended Capabilities

Version History

Introduced before R2006a

expand all