This example shows how to access the contents of a structure
array. To run the code in this example, load several variables into
a scalar (1-by-1) structure named
S = load('clown.mat')
S = struct with fields: X: [200x320 double] map: [81x3 double] caption: [2x1 char]
The variables from the file (
map) are now fields in the struct.
Access the data using dot notation of the form
For example, pass the numeric data in field
To access part of a field, add indices as appropriate for the
size and type of data in the field. For example, pass the upper left
X to the
upperLeft = S.X(1:50,1:80); image(upperLeft);
If a particular field contains a cell array, use curly braces
to access the data, such as
Data in Nonscalar Structure Arrays
Create a nonscalar array by loading data from the file
a second element of array
S(2) = load('mandrill.mat')
Each element of a structure array must have the same fields.
S is a 1-by-2 array.
S = 1×2 struct array with fields: X map caption
For nonscalar structures, the syntax for accessing a particular
the clown image, specifying the index for the clown struct (
Add indices to select and redisplay the upper left corner of the field contents:
upperLeft = S(1).X(1:50,1:80); image(upperLeft)
You can index into part of a field only when you refer to a
single element of a structure array. MATLAB® does not support
statements such as
S(1:2).X(1:50,1:80), which attempt
to index into a field for multiple elements of the structure.