zaro

How to Declare a Cell Array in MATLAB

Published in MATLAB Cell Array 3 mins read

You can declare a cell array in MATLAB using two primary methods: the {} operator or the cell function. These methods allow you to create containers that can hold different types of data in each element, unlike regular arrays which must contain data of the same type.

Cell arrays are incredibly versatile in MATLAB because they can store numbers, text, other arrays, structures, or even other cell arrays within a single variable.

Method 1: Using the {} Operator

This is often the most intuitive way to create a cell array, especially when you know the initial contents. You list the elements inside curly braces {}, separating them with commas or spaces. Each element you place inside the braces becomes a cell in the array.

  • Creating a 1xN or Nx1 Cell Array:

    • Separate elements with commas , for a row cell array.
    • Separate elements with semicolons ; for a column cell array.

    Examples:

    • Row cell array:
      myCellRow = {1, 'hello', [1 2 3], true};
    • Column cell array:
      myCellCol = {'apple'; 'banana'; 100};
  • Creating a 2D or Multidimensional Cell Array:

    • Use commas to separate elements within a row.
    • Use semicolons to separate rows.

    Example:

    myCellMatrix = {'Name', 'Age'; 'Alice', 30; 'Bob', 25};

Using the {} operator is ideal for creating cell arrays with a small, known number of initial elements.

Method 2: Using the cell Function

The cell function is primarily used to preallocate an empty cell array of a specific size. Preallocating can be more efficient, especially when building large cell arrays element by element within a loop. It creates a cell array where each cell initially contains an empty matrix [].

  • Syntax: cell(dim1, dim2, ..., dimN)

    • dim1, dim2, ... specify the dimensions of the cell array.

    Examples:

    • Create an empty 2x3 cell array:
      emptyCellArray = cell(2, 3);

      This creates a 2x3 grid where each cell is [].

    • Create an empty 1x5 cell array:
      emptyCellRow = cell(1, 5);

You would typically then populate the cells of the preallocated array using curly brace indexing {}:

myCell = cell(2, 2); % Create a 2x2 empty cell array
myCell{1, 1} = 'first cell';
myCell{1, 2} = 123;
myCell{2, 1} = [4 5 6];
myCell{2, 2} = {10, 20}; % A cell within a cell!

This method is particularly useful when you need a cell array of a certain size but don't know all the contents upfront, or when you need to build it iteratively.

Summary of Methods

Here's a quick overview:

Method Description Primary Use Case Example (2x2)
{} Operator Define elements directly within curly braces. Creating with known, initial contents. {'a', 1; true, [1 2]}
cell function Create an empty cell array of a specified size. Preallocating for iterative population or large arrays. cell(2, 2) (creates 2x2 of [])

In summary, you can create a cell array in two ways: use the {} operator or use the cell function. Choosing the right method depends on whether you are defining the contents upfront or preallocating space.