MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

2 Data Types of an array without regard to the contents of those cells, use cell indexing. This type of indexing is denoted by the parentheses operator ( ). Use cell indexing to assign any set of cells to another variable, creating a new cell array. Creating a New Cell Array from an Existing One • Content indexing gives you access to the contents of a cell. You can work with individual elements of an array within a cell, but you can only do so for one cell at a time. This indexing uses the curly brace operator { }. Displaying Parts of the Cell Array. Using the example cell array A, you candisplayinformationonthefirstrow of cells using cell indexing. (The MATLAB colon operator functions the same when used with cell arrays as it does with numeric arrays): A(1,:) ans = [3x5 double] [3.1416] [1x2 struct] To display the contents of these cells, use content indexing: A{1,:} ans = 19.0026 9.7196 9.1294 8.8941 18.4363 4.6228 17.8260 0.3701 12.3086 14.7641 12.1369 15.2419 16.4281 15.8387 3.5253 ans = 3.1416 ans = 1x2 struct array with fields: name 2-102

Cell Arrays billing test In assignments, you can use content indexing to access only a single cell, not a subset of cells. For example, the statements A{1,:} = value and B = A{1,:} are both invalid. However, you can use a subset of cells any place you would normally use a comma-separated list of variables (for example, as function inputs or when building an array). See “Replacing Lists of Variables with Cell Arrays” on page 2-108 for details. Assigning Cells. For cell indexing, assign the double array cell to X: X = A(1,1) X = [3x5 double] X isa1-by-1cellarray: whos X Name Size Bytes Class X 1x1 180 cell For content indexing, assign the contents of the first cell of row 1 to Y: Y = A{1,1} Y = 19.0026 9.7196 9.1294 8.8941 18.4363 4.6228 17.8260 0.3701 12.3086 14.7641 12.1369 15.2419 16.4281 15.8387 3.5253 Y is a 3-by-5 double array whos Y Name Size Bytes Class Y 3x5 120 double Assigning Multiple Cells. Assigning multiple cells with cell indexing is similar to assigning a single cell. MATLAB creates a new cell array, each cell of which contains a cell array. 2-103

2 Data Types<br />

of an array without regard to the contents of those cells, use cell indexing.<br />

This type of indexing is denoted by the parentheses operator ( ).<br />

Use cell indexing to assign any set of cells to another variable, creating<br />

a new cell array.<br />

Creating a New Cell Array from an Existing One<br />

• Content indexing gives you access to the contents of a cell. You can work<br />

with individual elements of an array within a cell, but you can only do so<br />

for one cell at a time. This indexing uses the curly brace operator { }.<br />

Displaying Parts of the Cell Array. Using the example cell array A, you<br />

candisplayinformationonthefirstrow of cells using cell indexing. (The<br />

<strong>MATLAB</strong> colon operator functions the same when used with cell arrays as it<br />

does with numeric arrays):<br />

A(1,:)<br />

ans =<br />

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

To display the contents of these cells, use content indexing:<br />

A{1,:}<br />

ans =<br />

19.0026 9.7196 9.1294 8.8941 18.4363<br />

4.6228 17.8260 0.3701 12.3086 14.7641<br />

12.1369 15.2419 16.4281 15.8387 3.5253<br />

ans =<br />

3.1416<br />

ans =<br />

1x2 struct array with fields:<br />

name<br />

2-102

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

Saved successfully!

Ooh no, something went wrong!