Programming |
Nesting Structures
A structure field can contain another structure, or even an array of structures. Once you have created a structure, you can use the struct
function or direct assignment statements to nest structures within existing structure fields.
Building Nested Structures with the struct Function
To build nested structures, you can nest calls to the struct
function. For example, create a 1-by-1 structure array:
A = struct('data', [3 4 7; 8 0 1], 'nest',... struct('testnum', 'Test 1', 'xdata', [4 2 8],... 'ydata', [7 1 6]));
You can build nested structure arrays using direct assignment statements. These statements add a second element to the array:
A(2).data = [9 3 2; 7 6 5]; A(2).nest.testnum = 'Test 2'; A(2).nest.xdata = [3 4 2]; A(2).nest.ydata = [5 0 9];
Indexing Nested Structures
To index nested structures, append nested field names using dot notation. The first text string in the indexing expression identifies the structure array, and subsequent expressions access field names that contain other structures.
For example, the array A
created earlier has three levels of nesting:
A(1)
, use A(1).nest
.
xdata
field in the nested structure in A(2)
, use A(2).nest.xdata
.
ydata
field in A(1)
, use A(1).nest.ydata(2)
.
Organizing Data in Structure Arrays | Function Summary |
© 1994-2005 The MathWorks, Inc.