Main Content

Cell Arrays

Arrays that can contain data of varying types and sizes

A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. For instance,

c = {42, rand(5), "abcd"}

returns

c =
  1×3 cell array
    {[42]}    {5×5 double}    {["abcd"]}

To access the contents of a cell, enclose indices in curly braces, such as c{1} to return 42 and c{3} to return "abcd". For more information, see Access Data in Cell Array.

Cell arrays are useful for nontabular data that you want to access by numeric index. If you have tabular data, such as data from a spreadsheet, use table or timetable instead. If your data is text only, use string.

Functions

expand all

cellCell array

From Cell

cell2matConvert cell array to ordinary array of the underlying data type
cell2structConvert cell array to structure array
cell2tableConvert cell array to table

To Cell

cellstrConvert to cell array of character vectors
mat2cellConvert array to cell array whose cells contain subarrays
num2cellConvert array to cell array with consistently sized cells
struct2cellConvert structure to cell array
table2cellConvert table to cell array

Determine Type

iscellDetermine if input is cell array
iscellstrDetermine if input is cell array of character vectors
celldispDisplay cell array contents
cellfunApply function to each cell in cell array
cellplotGraphically display structure of cell array

Topics

What Is a Cell Array?

A cell array is a data type with indexed data containers called cells. Each cell can contain any type of data.

Create Cell Array

Create a cell array by using the {} operator or the cell function.

Access Data in Cell Array

Read and write data from and to a cell array.

Pass Contents of Cell Arrays to Functions

These examples show several ways to pass data from a cell array to a function that does not recognize cell arrays as inputs.

Preallocate Memory for Cell Array

Initialize and allocate memory for a cell array.

Cell vs. Structure Arrays

This example compares cell and structure arrays and shows how to store data in each type of array. Cell and structure arrays enable you to store data of different types and sizes.