2.6 Cell Arrays

The cell array is an array in which each element is a bin, or cell, which can contain an array. You can store different classes of arrays in a cell array, and you can group data sets that are related but have different dimensions. You access cell arrays’ using the same index 109 operations used with ordinary arrays.
This is-the only section in the text that uses cell arrays. Coverage of this section is therefore optional. Some more advanced MATLAB application, such as those found in some of the toolboxes, do use cell arrays .

Creating Cell Arrays
You can create a cell array by using assignment statements or by using the cell function (see Table 2.6-1). You can assign data-to the cells by using either cell indexing or content indexing. To use cell indexing, enclose in parentheses


the cell subscripts on the left side of the assignment statement and use the standard array notation. Enclose the cell contents on the right side of the assignment statement in braces {}.

The cellplot function produces a graphical display of the cell array’s contents in the form of a grid. Type cellplot (A) to see this display for the cell array A. Use commas or spaces with braces to indicate columns of cells and use
semicolons to indicate rows of cells  ( just as with numeric arrays). For example, typing


You can preallocate empty cell arrays of a specified size by using the cell function. For example, type C = cell (3,5) to create the 3 x 5 cell array C and fill it with empty matrices. Once the array has been defined in this way, you can use assignment statements to enter the coritents of the cells. For example, type C (2,4) = {[ 6, -3,7]} to put the I x 3 array in cell”(2,4) and type C (1 , 5) = {1: 10} to put the numbers from I to 10 in cell (l,5). Type
C (3,4) = I ‘ 30 mph’} to put the string in cell (3,4). Do not name a cell array with the same name as a previously used numeric array without first using the clear command to clear the name. Otherwise, MATLAB will generate an error. In addition, MATLAB does not clear a cell array when you make a single assignment to it. You can  determine if an array is a cell array by using the is cell function. You can convert a numeric array to a cell array by using the num2cell function.

Accessing Cell Arrays
You can access the contents of a cell array by using either cell indexing or content indexing. For example, to use cell indexing to place the contents of cell (3,4) of

the array C in the new variable Speed, type Speed = C (3, 4) . To place the contents of the cells in rows 1 to 3, columns 2 to 5 in the new cell array D, type D = C ( 1 : 3 , 2 : 5 ) . The new cell array D wilI have three rows, four columns, and 12 arrays. To use content indexing to access some or all of the contents in a single cell, enclose the cell index expression in braces to indicate that you are assigning the contents, not the ceIls themselves, to a new variable. For example, typing Speed = C{3, 4} assigns the contents’ 30 mph ‘ in ceIl (3,4 to the variable Speed. You cannot use content indexing to retrieve the contents of more than one ceIl at a time. For example, the statements G = C {1, :} and C {1, :} = var, where var is some variable, are both” invalid. You can access subsets of a cell’s contents. For example, to obtain the second element in the 1 x 3-row vector in the (2,4) cell of array C and assign it to the variable r,you type r = C{2,4} (l,2).The result is r = -3. The deal function accesses elements of a range of cells in a cell array. For example, with the preceding cell array B, x and y can be assigned to the elements
in row 7 of B as foIlows:

»[x,y] = deal(B{2, :})
7 2

Using Cell Arrays
You can use cell arrays in comma-separated lists just as you would use ordinary MATLAB variables. For example, suppose you create the I x 4 ceIl array H by typing H = {[2,4,8], [6,-8,3], [2:6], [9,2,5]}; The expression H{2 : 4} is equivalent to a comma-separated list of the second through fourth cells in H. To create a numeric array J from the first, second, and fourth cells in the cell array H, you type J = [H{1}; H{2}; H{4})


You can also use cell arrays in this manner in function input and output lists. and you can store the results in another cell array, say, K. For example,
»[K{1:2}) = max(J)
K =
[1×3 double) [1×3 double)
type K{ I} to see the maximum values; type K{ 2} to see the corresponding indices.
»K{1} ans = 9 4 8 »K{2 }

ans =
3 1 1
You can apply functions and operators to cell contents. For example, suppose you create the 3 x.2 cell array L by typing
L = {[2,4,8), [6,-8,3); [2:6), [9,2,5); [1,4,5), (7,5,2)};
Then, for example:
»max (L{3, 2})
ans =
Nested cell arrays have cells that contain cell arrays, which may also contain cell arrays, and so on. To create nested arrays, you can use nested braces, the ce 11 function, or assignment statements. For example:
/ N(l,l) = (.[2,7,5)}; .
N( I, 2) = {{ [5, 9, 1; 4, 8, 0), r Case 1′; {5, 8}, [7, 3) }};
Typing N gives the result
[lx3 double) {2×2 cell}
The following steps create the same array N using the cell function. The method assigns the output office 11 to an existing cell.

% First create an empty lx2 cell array.
N = cell(1,2)
% Then create a 2×2 cell array inside N(l,2).
N(l,2) = {cell(2,2)}
% Then fill N using assignment statements.
N(l,l) = {[2,7,5)}; /
N{ 1, 2} (1,’1) = {[ 5 I 9 I 1; 4, 8, 0] }
N{I, 2} (1,2) = r : Case I’}

N{1,2}{2,1}(1) = {5}
N{1,2}{2,1}(2) = {8}
N{1,2}(2,2) = {[7,3]}
Note that braces are used for subscripts to access cell contents until the lowest “layer” of subscripts is reached. Then parentheses are used because the lowest layer does not contain cell arrays. As a final example, suppose you create the 3 x 2 cell array H by typing .
H = {[2,4,8], [6,-8,3]; [2:6], [9,2,5]; [1,4,5], [7,5,2]}i
You can create a numeric array J from the cell array H by typing
J = [H{l,l}i H{1,2}i H{2,2}]
The result is
[2 4 8] J = 6 -8 3
9 2 5
Typing H{2 : 3, :} displays the arrays in the second and third rows.
»H {2 : 3 , : }
2 3 4 5
9 2 5
1 4 5
7 5 2

Test Your Understanding
T2.6-1 Create the following cell array:

A = {[1:4], [0, 9, 2]” [2:5], [6:8]} (?[

What is A {l:2)? What is [A{2}i A{4}]?What is min [A{2},
A{ 3}?

Share This