Skip to main content

Registration is now open for this year's LibreFest! Join us virtually the week of July 13.

Register here
Engineering LibreTexts

8.4: Cell Arrays

  • Page ID
    136684
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    What Is a Cell Array?

    A cell array is an array whose elements are called cells. Each cell can contain almost anything: a number, a character vector, a string, a vector, a matrix, a logical value, or even another cell array. This makes cell arrays very flexible.

    The key visual idea is that each location in the array is like a little container. The container is the cell. The value inside the container is the content of the cell.

    To create a cell array, use curly braces instead of square brackets.

    cellRow = {23, 'a', 1:2:9, 'hello'}

    This creates a 1-by-4 cell array. The first cell contains the number 23. The second cell contains the character a. The third cell contains the vector [1 3 5 7 9]. The fourth cell contains the character vector hello.

    You can also create a two-dimensional cell array:

    cellMat = {23, 'a'; 1:2:9, 'hello'}

    This creates a 2-by-2 cell array. Just like a regular matrix, semicolons separate rows and commas or spaces separate columns.

    Key Idea

    Square brackets create ordinary arrays. Curly braces create cell arrays.

     

    Why Use a Cell Array?

    Cell arrays are useful when the position of the data tells you what it means. For example, imagine a car dealership wants to store basic information about cars for sale. Each car has a vehicle ID, make, model, and mileage. These pieces of information do not all have the same data type.

    carArray = {'J4568933',  'Ford',      'Focus',    345;
                'A66789901',  'Chevrolet', 'Corvette', 1000;
                'M99899923',  'Toyota',    'RAV4',     200}

    In this cell array, each row represents one car. Each column represents one type of information.

    Column

    Meaning

    Data type

    1

    Vehicle ID

    Character vector or string

    2

    Make

    Character vector or string

    3

    Model

    Character vector or string

    4

    Mileage

    Numeric

     

    A good habit is to organize a two-dimensional cell array so that each column contains the same type of information. This makes the data easier to read and easier to process later.

    Best Practice

    When using a 2D cell array as a small database, let each row represent one item and each column represent one attribute. Keep the same kind of information in each column.

     

    Creating an Empty Cell Array and Filling It

    Sometimes you may not want to write all of the data at once. You can first create an empty cell array using the cell function, and then fill in the cells one at a time.

    Example \(\PageIndex{1}\)

    myCellArray = cell(2,2);

    myCellArray{1,1} = 23;
    myCellArray{1,2} = "hey";
    myCellArray{2,1} = 3.1;
    myCellArray{2,2} = 'you';

    The first line creates a 2-by-2 cell array whose cells are initially empty. The next four lines place values inside the individual cells.

    There is another way to fill a cell array. Instead of using curly braces on the left side, you can use parentheses on the left side and place a cell on the right side.

    Example \(\PageIndex{2}\)

    yourCellArray = cell(2,2);

    yourCellArray(1,1) = {23};
    yourCellArray(1,2) = {"hey"};
    yourCellArray(2,1) = {3.1};
    yourCellArray(2,2) = {'you'};

    Both approaches can create the same final cell array, but they do not mean exactly the same thing. The distinction between curly braces and parentheses is one of the most important ideas in this chapter.

    Curly Braces vs. Parentheses

    This is the part of cell arrays that beginners often find confusing, so let’s slow down and be very clear.

    When you use curly braces with a cell array, MATLAB opens the cell and gives you the contents inside the cell. When you use parentheses with a cell array, MATLAB gives you a smaller cell array.

    Example \(\PageIndex{3}\)

    cellMat = {23, 'a'; 1:2:9, 'hello'};

    value_1 = cellMat{1,1};    % x is the number 23
    value_2 = cellMat(1,1);    % y is a 1-by-1 cell array containing 23

    value_3 = cellMat{2,1}(4)

    Expression

    What MATLAB returns

    Plain-language meaning

    cellMat{1,1}

    The contents of the cell

    Open the box and take out what is inside

    cellMat(1,1)

    A cell array

    Take the box itself

    cellMat{2,1}(4)

    The fourth element inside the vector stored in cell (2,1)

    Open the box, then index the vector inside

     

    Caution

    If you want the value inside a cell, use curly braces. If you want a smaller cell array, use parentheses. Many cell array errors happen because the wrong type of indexing is used.

     

    Modifying Cell Array Elements

    You can modify a cell array by assigning a new value into a cell. For example:

    cellMat{2,2} = 'bye';

    This changes the content of the cell at row 2, column 2 from hello to bye. Notice that curly braces are used because we are changing the contents of that cell.

    You can also change an entire row or column using parentheses when the replacement is also a cell array. For example:

    carArray(1,:) = {'J4568933', 'Ford', 'Focus', 500};

    This replaces the entire first row of carArray with a new row of cells.

     

    Useful Cell Array Functions

    MATLAB includes many built-in functions for working with cell arrays. You do not need to memorize every function immediately, but you should become familiar with the common ones. Click each function name to learn more about it in the MATLAB documentation.

    Function

    Purpose

    cell

    Creates a cell array of empty matrices

    iscell

    Checks whether an input is a cell array

    cellstr

    Converts text to a cell array of character vectors

    num2cell

    Converts a numeric array into a cell array

    cell2mat

    Converts a cell array of compatible numeric data into a numeric array

    mat2cell

    Breaks a matrix into cells of specified sizes

    cell2struct

    Converts a cell array to a structure array

    struct2cell

    Converts a structure to a cell array

    celldisp

    Displays the contents of a cell array

    cellplot

    Graphically displays the organization of a cell array

     

    Example \(\PageIndex{4}\)
    % Converting a cell array to a string array
    
    A = ["Past", "Present", "Future"];
    
    C = cellstr(A)

     

    Deleting Elements from Cell Arrays

    Deleting from a one-dimensional cell array works much like deleting from a regular vector. You use parentheses and assign an empty array.

    Example \(\PageIndex{5}\)

    cellRow = {23, 't', 1:2:9, 'hello'};
    cellRow(2) = [];

    Solution

    After this code runs, the second cell is removed, and the cell array becomes shorter.

    cellRow = {23, 1:2:9, 'hello'}

     

    For a two-dimensional cell array, you cannot delete just one individual cell and leave a hole in the array. MATLAB arrays must remain rectangular. Instead, you delete an entire row or an entire column.

    Example \(\PageIndex{6}\)

    carArray = {'Ford',      'Focus',    345;
                'Chevrolet', 'Corvette', 1000;
                'Toyota',    'RAV4',     200};

    carArray(:,3) = [];   % Delete the third column

     

    Solution

    This removes the entire third column from the cell array.

    carArray = {'Ford',      'Focus',    345;
                'Chevrolet', 'Corvette', 1000}

    Checkpoint 1

    What is the difference between cellRow{2} and cellRow(2)? Write a one-sentence answer before continuing.

     

    Exercise \(\PageIndex{1}\)

    What is the difference between cellRow{2} and cellRow(2)?


    8.4: Cell Arrays is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?