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 read a single value, specify the index of the target cell using cell indexing or content indexing. If you use cell indexing, the cell is returned as a 1-by-1 cell array. If you use content indexing, the content of the cell is returned.

Enclose indices in `()` to use cell indexing, or in `{}` to use content indexing.
``````% First cell
resultExample1 = A(1);
whos resultExample1;``````
``````% Content of the first cell
resultExample2 = A{1};
whos resultExample2;``````

Note: If the index is out of range, MATLAB generates an error.

## Multiple values

If you use cell indexing to read multiple values from a cell array, the result is a cell array. If you use content indexing, the result is a comma-separated list.

Prefer cell indexing over content indexing to read multiple values from cell arrays.
``````% Cells in the first column
nameCells = A(1:2);
whos nameCells;``````
``````% Content of the cells in the first column
[name1,name2] = A{1:2};``````
``````% Content of the cells in the third column
ages = [A{5:6}];``````
``````% Undesired: Strings are concatenated
buggyNames = [A{1:2}];``````

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

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

1. This book was the winner of a 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. Store in a variable the xorigin of the first bounding box
2. Store in a single variable the label of each bounding box
3. Calculate the area of the second bounding box
4. Calculate the area of each bounding box using a single statement

## Source code

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