23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Cell Arrays<br />

Theexamplesinthissectionillustratehow to access the different components<br />

of a cell array. All of the examples use the following six-cell array which<br />

consists of different data types.<br />

First, build the individual components of the example array:<br />

rand('state', 0); numArray = rand(3,5)*20;<br />

chArray = ['Ann Lane'; 'John Doe'; 'Al Smith'];<br />

cellArray = {1 4 3 9; 0 5 8 2; 7 2 9 2; 3 3 1 4};<br />

logArray = numArray > 10;<br />

stArray(1).name = chArray(1,:);<br />

stArray(2).name = chArray(2,:);<br />

stArray(1).billing = 28.50;<br />

stArray(2).billing = 139.72;<br />

stArray(1).test = numArray(1,:);<br />

stArray(2).test = numArray(2,:);<br />

and then construct the cell array from these components using the { }<br />

operator:<br />

A = {numArray, pi, stArray; chArray, cellArray, logArray};<br />

To see what size and type of array occupies each cell in A, type the variable<br />

name alone:<br />

A<br />

A =<br />

[3x5 double] [ 3.1416] [1x2 struct ]<br />

[3x8 char ] {4x4 cell} [3x5 logical]<br />

Manipulating Cells and the Contents of Cells<br />

When working with cell arrays, you have a choice of selecting entire cells of an<br />

array to work with, or the contents of those cells. The first method is called<br />

cell indexing; thesecondiscontent indexing:<br />

• Cell indexing enables you to work with whole cells of an array. You can<br />

access single or multiple cells within the array, but you cannot select<br />

anything less than the complete cell. If you want to manipulate the cells<br />

2-101

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

Saved successfully!

Ooh no, something went wrong!