If you want to process each element of an array with the same operation, use the arrayfun function.
For example, count the number of elements in field f of each structure in array s:
numElements = arrayfun(@(x) numel(x.f), s)
The syntax @(x) creates an anonymous function. This code calls the numel function for each element
of array s, such as numel(s(1).f), and returns
numElements =
1
3
9
For related information, see:
• “Comma-Separated Lists” on page 2-79
• “Anonymous Functions” on page 20-20
11 Structures
11-14
For example, count the number of elements in field f of each structure in array s:
numElements = arrayfun(@(x) numel(x.f), s)
The syntax @(x) creates an anonymous function. This code calls the numel function for each element
of array s, such as numel(s(1).f), and returns
numElements =
1
3
9
For related information, see:
• “Comma-Separated Lists” on page 2-79
• “Anonymous Functions” on page 20-20
11 Structures
11-14
