Accessing Data in Cell Arrays



Prev TutorialNext Tutorial

There are two ways to refer to the elements of a cell array −
  • Enclosing the indices in first bracket (), to refer to sets of cells
  • Enclosing the indices in braces {}, to refer to the data within individual cells
When you enclose the indices in first bracket, it refers to the set of cells.
Cell array indices in smooth parentheses refer to sets of cells.
For example:
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c(1:2,1:2)
MATLAB will execute the above statement and return the following result −
ans = 
{
  [1,1] = Red
  [2,1] =  1
  [1,2] = Blue
  [2,2] =  2
}
You can also access the contents of cells by indexing with curly braces.
For example −
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c{1, 2:4}
MATLAB will execute the above statement and return the following result −
ans = Blue
ans = Green
ans = Yellow

Prev TutorialNext Tutorial