MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 152 and 153: 2 Data Types Note You can specify m
- Page 154 and 155: 2 Data Types operator, specifying t
- Page 156 and 157: 2 Data Types Valid Syntax Invalid S
- Page 158 and 159: 2 Data Types A = 'fate'; B = 'cake'
- Page 160 and 161: 2 Data Types while (any(remainder))
- Page 162 and 163: 2 Data Types Converting from String
- Page 164 and 165: 2 Data Types Function Summary MATLA
- Page 166 and 167: 2 Data Types Functions to Determine
- Page 168 and 169: 2 Data Types Date Strings There are
- Page 170 and 171: 2 Data Types • The day field is a
- Page 172 and 173: 2 Data Types datestr(d) ans = 01-Ma
- Page 174 and 175: 2 Data Types Utility Functions (Con
- Page 176 and 177: 2 Data Types • “Applying Functi
- Page 178 and 179: 2 Data Types where the arguments ar
- Page 180 and 181: 2 Data Types You can access subarra
- Page 182 and 183: 2 Data Types Dynamic Field Names Ex
- Page 184 and 185: 2 Data Types Deleting Fields from S
- Page 186 and 187: 2 Data Types plot(mercury, 'b') plo
- Page 188 and 189: 2 Data Types There are at least two
- Page 190 and 191: 2 Data Types Element-by-element org
- Page 192 and 193: 2 Data Types Nesting Structures A s
- Page 194 and 195: 2 Data Types Cell Arrays A cell arr
- Page 196 and 197: 2 Data Types Operation Syntax Descr
- Page 198 and 199: 2 Data Types 3-by-3 Cell Array Note
- Page 200 and 201: 2 Data Types C5 = 'Jan' 'Feb' 'Mar'
- Page 204 and 205: 2 Data Types Createa1-by-2arraywith
- Page 206 and 207: 2 Data Types ans = 7 ans = 2 ans =
- Page 208 and 209: 2 Data Types 3 4 B = reshape(A, 6,
- Page 210 and 211: 2 Data Types A{1,1} = [1 2; 3 4]; A
- Page 212 and 213: 2 Data Types [5x5 double] {2x2 cell
- Page 214 and 215: 2 Data Types G = cell(1,16); for m
- Page 216 and 217: 2 Data Types Function Handles A fun
- Page 218 and 219: 2 Data Types MATLAB Classes All MAT
- Page 220 and 221: 2 Data Types 2-120
- Page 222 and 223: 3 Basic Program Components Symbol R
- Page 224 and 225: 3 Basic Program Components Note If
- Page 226 and 227: 3 Basic Program Components Another
- Page 228 and 229: 3 Basic Program Components Verifyin
- Page 230 and 231: 3 Basic Program Components function
- Page 232 and 233: 3 Basic Program Components Evaluati
- Page 234 and 235: 3 Basic Program Components Keywords
- Page 236 and 237: 3 Basic Program Components Hereares
- Page 238 and 239: 3 Basic Program Components Operator
- Page 240 and 241: 3 Basic Program Components To test
- Page 242 and 243: 3 Basic Program Components Function
- Page 244 and 245: 3 Basic Program Components 5 11 10
- Page 246 and 247: 3 Basic Program Components Advantag
- Page 248 and 249: 3 Basic Program Components MATLAB E
- Page 250 and 251: 3 Basic Program Components This M-f
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