Create a Cell Array

This example shows how to create a cell array using the {} operator or the cell function.

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

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

Like all MATLAB® arrays, cell arrays are rectangular, with the same number of cells in each row. myCell is a 2-by-3 cell array:

myCell = 
    [   1]    [            2]    [       3]
    'text'    [5x10x2 double]    {3x1 cell}

You also can use the {} operator to create an empty 0-by-0 cell array,

C = {}

If you plan to add values to a cell array over time or in a loop, you can create an empty n-dimensional array using the cell function:

emptyCell = cell(3,4,2)

emptyCell is a 3-by-4-by-2 cell array, where each cell contains an empty array, []:

emptyCell(:,:,1) = 
    []    []    []    []
    []    []    []    []
    []    []    []    []

emptyCell(:,:,2) = 

    []    []    []    []
    []    []    []    []
    []    []    []    []

Related Examples

More About

Was this topic helpful?