Access Elements of a Nonscalar Struct Array
This example shows how to access and process data from multiple elements of a
nonscalar structure array:
Create a 1-by-3 structure s with field f:
s(1).f = 1;
s(2).f = 'two';
s(3).f = 3 * ones(3);
Although each structure in the array must have the same number of fields and the same
field names, the contents of the fields can be different types and sizes. When you refer to
field f for multiple elements of the structure array, such as
s(1:3).f
or
s.f
MATLAB returns the data from the elements in a comma-separated list, which displays as
follows:
ans =
1
ans =
two
ans =
3 3 3
3 3 3
3 3 3
You cannot assign the list to a single variable with the syntax v = s.f because the fields
can contain different types of data. However, you can assign the list items to the same
number of variables, such as
[v1, v2, v3] = s.f;
or assign to elements of a cell array, such as
c = {s.f};
11 Structures