Cell Arrays and Cell Indexing in MATLAB

Cell indexing allows to reference cells in a cell array.


Create a cell array using the following code:

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

Cell indexing

Enclose linear, subscript or logical indices in parentheses to refer to a set of cells in a cell array. If you use cell indexing to read a cell array, the result is a cell array too. If you use cell indexing to write a cell array, the right part of the assignment must be a cell array or an empty array.

% First cell
resultExample1 = A(1);
whos resultExample1;
% Cells in the first column
resultExample2 = A(1:2);
whos resultExample2;
% Write the first cell
A(1) = {'Peter'};
% Wrong code, the new value is not a cell array
A(1) = 'Peter';

Note: If you use cell indexing to write a cell array and the new value is an empty array, the target cells are deleted from the cell array.

Further reading

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

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

    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.

Source code

I hope you have understood how cell indexing works in MATLAB. The source code developed in this tutorial is available at this page.


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