Académique Documents
Professionnel Documents
Culture Documents
Overview
Structures
>> a(3, 2) = 6;
Arrays of structures
What is a structure?
A structure is a data type in which individual data elements are
accessed by specifying a field name of the data element to
access.
The collection of fields in a structure is like a 1D cell array
except that we use field names rather than indices to access
data.
Each field of a structure can refer to data of any type.
We can access individual fields of a structure by specifying
the structure name followed by a period and then the field
name of interest.
There are two ways of generating the structure.
1. Adding one field at a time using assignment statements
2. All at once using the struct function
9/24/09
student
For example, imagine creating a structure called beam with the fields
length, sectionarea, material and manufacturer.
>>
>>
>>
>>
name
beam.length = 10.4;
beam.sectionarea = 0.03;
beam.material = 'Carbon Fibre'
beam.manufacturer = 'GLC'
address
number
John Smith
1First Ave,
Crawley
20007564
beam =
length:
sectionarea:
material:
manufacturer:
marks
10.4000
0.0300
'Carbon Fibre'
'GLC'
beam =
length:
sectionarea:
material:
manufacturer:
1.2000
0.0500
'steel'
'unknown'
[30,40,70]
For example:
>> fieldnames(beam)
ans =
'length'
'sectionarea'
'material'
'manufacturer'
9/24/09
Arrays of structures
Often we will want multiple instances of a single structure.
We can build up an array of structures. We call this array a
structure array.
Matlab allows a structure array to grow automatically.
>> beam(2).length = 5 % Beam is now a structure array.
% Set the length field of the second
% element of the structure array to 5.
beam =
1x2 struct array with fields:
length
sectionarea
material
manufacturer
10.4000
0.0300
'Carbon Fibre'
'GLC'
5
[]
[]
[]
9/24/09
For example:
>> beam(2).length
ans =
5
10.4
0.03
`Carbon
Fibre
`GLC
1.2
0.05
`steel
`unknown
9/24/09