Programming Previous page   Next Page

Creating Cell Arrays

You can create cell arrays by

Creating Cell Arrays with Assignment Statements

You can build a cell array by assigning data to individual cells, one cell at a time. MATLAB automatically builds the array as you go along. There are two ways to assign data to cells:

The various examples in this guide do not use one syntax throughout, but attempt to show representative usage of cell and content addressing. You can use the two forms interchangeably.

MATLAB displays the cell array A in a condensed form:

To display the full cell contents, use the celldisp function. For a high-level graphical display of cell architecture, use cellplot.

If you assign data to a cell that is outside the dimensions of the current array, MATLAB automatically expands the array to include the subscripts you specify. It fills any intervening cells with empty matrices. For example, the assignment below turns the 2-by-2 cell array A into a 3-by-3 cell array.

Cell Array Syntax: Using Braces

The curly braces {} are cell array constructors, just as square brackets are numeric array constructors. Curly braces behave similarly to square brackets, except that you can nest curly braces to denote nesting of cells (see Nesting Cell Arrays for details).

Curly braces use commas or spaces to indicate column breaks and semicolons to indicate row breaks between cells. For example,

results in

Use square brackets to concatenate cell arrays, just as you do for numeric arrays.

Preallocating Cell Arrays with the cell Function

The cell function allows you to preallocate empty cell arrays of the specified size. For example, this statement creates an empty 2-by-3 cell array:

Use assignment statements to fill the cells of B:

The cell function offers the most memory-efficient way of preallocating a cell array.

Memory Requirements for Cell Arrays

You do not necessarily need a contiguous block of memory to store a cell array. The memory for each cell needs to be contiguous, but not the entire array of cells.


Previous page  Cell Arrays Obtaining Data from Cell Arrays Next page

© 1994-2005 The MathWorks, Inc.