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.

2 Data Types<br />

[5x5 double] {2x2 cell}<br />

Note that the right side of the assignment is enclosed in two sets of curly<br />

braces. The first set represents cell (1,2) of cell array A. The second<br />

“packages“ the 2-by-2 cell array inside the outer cell.<br />

Building Nested Arrays with the cell Function<br />

To nest cell arrays with the cell function, assign the output of cell to an<br />

existing cell:<br />

1 Create an empty 1-by-2 cell array.<br />

A = cell(1,2);<br />

2 Create a 2-by-2 cell array inside A(1,2).<br />

A(1,2) = {cell(2,2)};<br />

3 Fill A, including the nested array, using assignments.<br />

A(1,1) = {magic(5)};<br />

A{1,2}(1,1) = {[5 2 8; 7 3 0; 6 7 3]};<br />

A{1,2}(1,2) = {'Test 1'};<br />

A{1,2}(2,1) = {[2-4i 5+7i]};<br />

A{1,2}(2,2) = {cell(1, 2)}<br />

A{1,2}{2,2}(1) = {17};<br />

Note the use of curly braces until the final level of nested subscripts. This is<br />

required because you need to access cell contents to access cells within cells.<br />

You can also build nested cell arrays with direct assignments using the<br />

statements shown in step 3 above.<br />

Indexing Nested Cell Arrays<br />

To index nested cells, concatenate indexing expressions. The first set of<br />

subscripts accesses the top layer of cells, and subsequent sets of parentheses<br />

access successively deeper layers.<br />

2-112

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

Saved successfully!

Ooh no, something went wrong!