Tutorial

How to Write Values to a Cell Array in MATLAB

A cell array is a data type that uses indexed containers called “cells” to store data of varying types and sizes.

Preparation

Create a cell array using the following code:

A = {'Peter Pan', 'M', 11; 'Wendy Darling', 'F', 12};

Single value

To write a single value, specify the index of the target cell using cell indexing or content indexing. If you use cell indexing, the new value must be a 1-by-1 cell array. If you use content indexing, the new value can be anything.

Enclose indices in () to use cell indexing, or in {} to use content indexing.
% Set the first cell
A(1) = {'Peter'};
% Set the content of the second cell
A{2} = 'Wendy';

Note: If the index is out of range, MATLAB expands the cell array as required. Unassigned cells are initialized to an empty array.

Multiple values (I)

If you use cell indexing to write multiple values, the right part of the assignment must be a cell array having the correct number of elements, or a scalar cell array. In the latter case, its value is assigned to all target cells.

Prefer cell indexing over content indexing to write multiple values to cell arrays.
% Set the cells in the second column
newSexes = {'Male','Female'};
A(3:4) = newSexes;
% Set the cells in the third column
newAges = {12,13};
A(5:6) = newAges;
% Set the cells in the third column to the same value
A(5:6) = {14};

Multiple values (II)

If you use content indexing to write multiple values, the right part of the assignment must generate a comma-separated list. To set the content of a group of cells to the same value, use the deal function.

% Set the content of the cells in the second column
origSexes = {'M','F'};
[A{3:4}] = origSexes{:};
% Set the content of the cells in the third column
origAges = {11,12};
[A{5:6}] = origAges{:};
% Set the content of the cells in the third column to the same value
[A{5:6}] = deal(14);

Note: In all examples I used linear indices, but you can use subscripts and logical indices too.

Further reading

I recommend the following books to learn more on cell arrays in MATLAB.

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a 2017 Textbook Excellence Award. Cell arrays, and other data types, are explained clearly in the eighth chapter.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Chapter 11 explains excellently cell arrays, structure arrays, and character arrays, among other data types.

I also recommend the other tutorials in this series.

Exercise

A Minimum Bounding Box (MBR) is the minimum rectangle that encloses a group of pixels. MBRs are usually defined by the location of the upper-left corner, a width, and a height.

Create a cell array using the following code. Values in each row are ordered as follows: label, xorigin, yorigin, width, height.

mbrs = {'MBR1', 0, 0, 10, 5; 'MBR2', 10, 5, 7, 8; 'MBR3', 7, 9, 3, 6};
  1. Set the width of the first bounding box to 9
  2. Set the height of the first two bounding boxes to 4 and 5, respectively
  3. Set the xorigin of the last two bounding boxes to 0
  4. Set the label of each bounding box to the string ‘Box ID’

Source code

I hope you have learned how to write values to cell arrays in MATLAB. The source code developed in this tutorial is available at this page.

Share

Your friends may need this knowledge to complete a task. Share this tutorial.