structfun
Applies a function to each field of a scalar struct.
Category: Language Fundamentals > Data Types
Syntax
A = structfun(func, S)
Calls func on every field value and collects the results in a column vector.
B = structfun(func, S, 'UniformOutput', false)
Returns a struct with the same field names holding the results.
Description
structfun is the struct counterpart of cellfun and arrayfun. It is helpful for summaries over a set of named values, such as structfun(@numel, s) for the size of each field.
When the function returns non-scalar results (strings, vectors), set 'UniformOutput' to false; otherwise an error is raised.
Input arguments
func- Function handle that takes one field value.
S- Scalar struct.
'UniformOutput'true(default) requires every result to be a scalar;falseallows any result and returns a struct.
Output arguments
A- Column vector with one element per field, in field order.
Example
Column result and struct result
s.a = 1; s.b = 4; s.c = 9;
structfun(@sqrt, s)
r = structfun(@(v) v*2, s, 'UniformOutput', false)
Output
ans = 3×1
1
2
3
r = struct with fields:
a: 2
b: 8
c: 18