23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Cell Arrays<br />

Building Cell Arrays with Concatenation<br />

There are two ways that you can construct a new cell array from existing<br />

cell arrays:<br />

• Concatenateentirecellarraystoindividual cells of the new array. For<br />

example, join three cell arrays together to build a new cell array having<br />

three elements, each containing a cell array. This method uses the curly<br />

brace { } operator.<br />

• Concatenate the contents of the cells into a new array. For example, join<br />

cell arrays of size m-by-n1, m-by-n2, andm-by-n3 together to yield a new<br />

cell array that is m-by-(n1+n2+n3) in size. This method uses the square<br />

bracket [ ] operator.<br />

Here is an example. First, create three 3–row cell arrays of different widths.<br />

C1 = {'Jan' 'Feb'; '10' '17'; uint16(2004) uint16(2001)};<br />

C2 = {'Mar' 'Apr' 'May'; '31' '2' '10'; ...<br />

uint16(2006) uint16(2005) uint16(1994)};<br />

C3 = {'Jun'; '23'; uint16(2002)};<br />

This creates arrays C1, C2, andC3:<br />

C1 C2 C3<br />

'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun'<br />

'10' '17' '31' '2' '10' '23'<br />

[2004] [2001] [2006] [2005] [1994] [2002]<br />

Use the curly brace operator to concatenate entire cell arrays, thus building<br />

a 1-by-3 cell array from the three initial arrays. Each cell of this new array<br />

holds its own cell array:<br />

C4 = {C1 C2 C3}<br />

C4 =<br />

{3x2 cell} {3x3 cell} {3x1 cell}<br />

Now use the square bracket operator on the same combination of cell arrays.<br />

This time <strong>MATLAB</strong> concatenates the contents of the cells together and<br />

produces a 3-by-6 cell array:<br />

C5 = [C1 C2 C3]<br />

2-99

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!