MATLAB Programming
MATLAB Programming MATLAB Programming
2 Data Types • “Applying Functions and Operators” on page 2-84 • “Writing Functions to Operate on Structures” on page 2-85 • “Organizing Data in Structure Arrays” on page 2-86 • “Nesting Structures” on page 2-92 • “Function Summary” on page 2-93 Building Structure Arrays You can build structures in two ways: • Using assignment statements • Using the struct function Building Structure Arrays Using Assignment Statements You can build a simple 1-by-1 structure array by assigning data to individual fields. MATLAB automatically builds the structure as you go along. For example, create the 1-by-1 patient structure array shown at the beginning of this section: patient.name = 'John Doe'; patient.billing = 127.00; patient.test = [79 75 73; 180 178 177.5; 220 210 205]; Now entering patient at the command line results in name: 'John Doe' billing: 127 test: [3x3 double] patient is an array containing a structure with three fields. To expand the structure array, add subscripts after the structure name: patient(2).name = 'Ann Lane'; patient(2).billing = 28.50; 2-76
Structures patient(2).test = [68 70 68; 118 118 119; 172 170 169]; The patient structure array now has size [1 2]. Note that once a structure array contains more than a single element, MATLAB does not display individual field contents when you type the array name. Instead, it shows a summary of the kind of information the structure contains: patient patient = 1x2 struct array with fields: name billing test You can also use the fieldnames function to obtain this information. fieldnames returns a cell array of strings containing field names. As you expand the structure, MATLAB fills in unspecified fields with empty matrices so that • All structures in the array have the same number of fields. • All fields have the same field names. For example, entering patient(3).name = 'Alan Johnson' expands the patient array to size [1 3]. Now both patient(3).billing and patient(3).test contain empty matrices. Note Field sizes do not have to conform for every element in an array. In the patient example, the name fields can have different lengths, the test fields can be arrays of different sizes, and so on. Building Structure Arrays Using the struct Function You can preallocate an array of structures with the struct function. Its basic form is strArray = struct('field1',val1,'field2',val2, ...) 2-77
- Page 126 and 127: 2 Data Types NaN MATLAB represents
- Page 128 and 129: 2 Data Types All available formats
- Page 130 and 131: 2 Data Types Integer Functions Func
- Page 132 and 133: 2 Data Types Infinity and NaN Funct
- Page 134 and 135: 2 Data Types 1 0 0 1 0 1 1 0 The MA
- Page 136 and 137: 2 Data Types A(B) ans = 5 15 30 35
- Page 138 and 139: 2 Data Types Characters and Strings
- Page 140 and 141: 2 Data Types name = ['Thomas R. Lee
- Page 142 and 143: 2 Data Types Phoenix length(strings
- Page 144 and 145: 2 Data Types • “Setting Field W
- Page 146 and 147: 2 Data Types Ordered Sequentially s
- Page 148 and 149: 2 Data Types An alternate syntax, t
- Page 150 and 151: 2 Data Types Precision is not usual
- 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 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 202 and 203: 2 Data Types of an array without re
- 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
2 Data Types<br />
• “Applying Functions and Operators” on page 2-84<br />
• “Writing Functions to Operate on Structures” on page 2-85<br />
• “Organizing Data in Structure Arrays” on page 2-86<br />
• “Nesting Structures” on page 2-92<br />
• “Function Summary” on page 2-93<br />
Building Structure Arrays<br />
You can build structures in two ways:<br />
• Using assignment statements<br />
• Using the struct function<br />
Building Structure Arrays Using Assignment Statements<br />
You can build a simple 1-by-1 structure array by assigning data to individual<br />
fields. <strong>MATLAB</strong> automatically builds the structure as you go along. For<br />
example, create the 1-by-1 patient structure array shown at the beginning of<br />
this section:<br />
patient.name = 'John Doe';<br />
patient.billing = 127.00;<br />
patient.test = [79 75 73; 180 178 177.5; 220 210 205];<br />
Now entering<br />
patient<br />
at the command line results in<br />
name: 'John Doe'<br />
billing: 127<br />
test: [3x3 double]<br />
patient is an array containing a structure with three fields. To expand the<br />
structure array, add subscripts after the structure name:<br />
patient(2).name = 'Ann Lane';<br />
patient(2).billing = 28.50;<br />
2-76