Apply function to each field of scalar structure
[A1,...,An] = structfun(func,S)
[A1,...,An] = structfun(func,S,Name,Value)
[
applies
the function specified by function handle A1,...,An
] = structfun(func
,S
)func
to
each field of scalar structure S
. Output arrays A1,...,An
,
where n
is the number of outputs from function func
,
contain the outputs from the function calls.
[
calls
function A1,...,An
] = structfun(func
,S
,Name,Value
)func
with additional options specified
by one or more Name,Value
pair arguments. Possible
values for Name
are 'UniformOutput'
or 'ErrorHandler'
.
|
Handle to a function that accepts a single input argument and
returns If function |
|
Scalar structure. |
Specify optional comma-separated pairs of Name,Value
arguments.
Name
is the argument
name and Value
is the corresponding
value. Name
must appear
inside single quotes (' '
).
You can specify several name and value pair
arguments in any order as Name1,Value1,...,NameN,ValueN
.
|
Logical value, as follows:
Default: | ||||||
|
Handle to a function that catches any errors that occur when MATLAB attempts
to execute function MATLAB calls the specified error-handling function with two input arguments:
|
|
Arrays that collect the If
If |
Create a scalar structure, and count the number of characters in each field.
s.f1 = 'Sunday'; s.f2 = 'Monday'; s.f3 = 'Tuesday'; s.f4 = 'Wednesday'; s.f5 = 'Thursday'; s.f6 = 'Friday'; s.f7 = 'Saturday'; lengths = structfun(@numel, s)
Shorten the text in each field of s
, created
in the previous example. Because the output is nonscalar, set UniformOutput
to false
.
shortNames = structfun(@(x) ( x(1:3) ), s, 'UniformOutput', false)
The syntax @(x)
creates an anonymous function.
Define and call a custom error handling function.
function result = errorfun(errorinfo, field) warning(errorinfo.identifier, errorinfo.message); result = NaN; end mystruct.f1 = 'text'; myresult = structfun(@(x) x^2, mystruct, 'ErrorHandler', @errorfun)
arrayfun
| cell2mat
| cellfun
| spfun
| splitapply