structfun — Apply a function to each field of a scalar struct.
structfun(func, S) applies func to each field value of scalar struct S. By default callback results must be scalar-uniform and are packed into a column array in field order.
Syntax
A = structfun(func, S)
A = structfun(func, S, "UniformOutput", tf)
A = structfun(func, S, "ErrorHandler", errfunc)
A = structfun(func, S, "UniformOutput", tf, "ErrorHandler", errfunc)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
func | Any | Yes | — | Function handle or builtin/function name. |
S | Any | Yes | — | Scalar struct input. |
UniformOutput | StringScalar | Yes | "UniformOutput" | Name-value key for uniform output mode. |
tf | LogicalArray | Yes | true | Whether callback outputs must be scalar-uniform. |
ErrorHandler | StringScalar | Yes | "ErrorHandler" | Name-value key for callback error handler. |
errfunc | Any | Yes | — | Error handler function handle or name. |
Returns
| Name | Type | Description |
|---|---|---|
A | Any | Mapped callback outputs. |
Returned values from structfun depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:structfun:InvalidInput | Input callback, struct argument, or name-value forms are invalid. | structfun: invalid input arguments |
RunMat:structfun:NotScalarStruct | Second argument is not a scalar struct. | structfun: second input must be a scalar struct |
RunMat:structfun:Internal | Internal field vector materialisation fails. | structfun: internal error |
RunMat:structfun:UniformOutput | UniformOutput mode requirements are violated. | structfun: uniform output contract violated |
RunMat:structfun:FunctionError | Callback execution or callback output arity fails. | structfun: callback execution error |
How structfun works
- The second input must be a scalar struct. Struct arrays are rejected rather than flattened implicitly.
- Callback execution uses RunMat's
fevalpath, so function handles, semantic closures, requested output count,UniformOutput,ErrorHandler, and GPU gather behavior follow ordinary callback invocation semantics. - With
"UniformOutput", true(default), every callback result must be scalar numeric, logical, or complex; results are packed into an N-by-1 array where N is the number of fields. - With
"UniformOutput", false, callback outputs may have mixed types or sizes and are returned as a scalar struct preserving the input field names. - When multiple outputs are requested, each callback must return that many outputs;
structfuncollects each output independently. "ErrorHandler", handlerreceives an error struct followed by the field value that triggered the failure.
Does RunMat run structfun on the GPU?
structfun is a host operation. GPU-resident field values are gathered before the callback runs and outputs are host-resident today.
Examples
Compute the length of each struct field
S = struct('a', 1:3, 'b', 10:11);
A = structfun(@length, S)Expected output:
A =
3
2Collect mixed callback outputs in a struct
S = struct('name', 'runmat', 'flag', true);
C = structfun(@class, S, 'UniformOutput', false)Expected output:
C =
struct with fields:
name: 'char'
flag: 'logical'Using structfun with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how structfun changes the result.
Run a small structfun example, explain the result, then change one input and compare the output.
FAQ
Does structfun modify the input struct?⌄
No. It reads the field values, invokes the callback, and returns a new array or cell array.
Can structfun return strings or structs?⌄
Yes, when "UniformOutput" is false. The result is a scalar struct with the same field names as the input. Uniform output mode accepts only scalar numeric, logical, or complex callback results.
Are GPU field values supported?⌄
Yes. GPU-resident field values are gathered before callback invocation; outputs are host-resident today.
Can structfun collect multiple callback outputs?⌄
Yes. When callers request multiple outputs, RunMat requests the same number from every callback invocation and returns one collected output per callback output.
Related Structs functions
fieldnames · getfield · isfield · orderfields · rmfield · setfield · struct
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how structfun is executed, line by line, in Rust.
- View the source for structfun in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.