struct — Create scalar structs or struct arrays from field/value inputs.
struct creates scalar structs and struct arrays from name/value pairs, copies existing structs, and expands equally shaped cell-valued fields.
Syntax
S = struct()
S = struct(template)
S = struct(field, value, ...)Inputs
template- Type
Any- Required
- Yes
- Description
- Existing struct/array template or empty array.
field- Type
PropertyName- Required
- Yes
- Description
- Field name.
value- Type
Any- Required
- Yes
- Description
- Field value or cell array.
name_value_pairs- Type
Any- Required
- Variadic
- Description
- Additional field/value pairs.
Returns
S- Type
Any- Description
- Scalar struct or struct array.
Errors
RunMat:struct:TooManyOutputs- When
- More than one output is requested.
- Message
struct: too many output arguments
RunMat:struct:InvalidSingleInput- When
- Single input is not a valid struct template.
- Message
struct: expected name/value pairs, an existing struct or struct array, or [] to create an empty struct array
RunMat:struct:NameValuePairs- When
- Arguments are not complete pairs.
- Message
struct: expected name/value pairs
Behavior
struct() creates a scalar struct with no fields. Name/value pairs create fields in insertion order; duplicate field names raise an error. Field names must be nonempty MATLAB identifiers within namelengthmax and cannot be language keywords.
When any value is a cell array, every cell-valued field must have the same shape. Each cell contributes to the corresponding struct-array element, while non-cell values are replicated across the array. struct([]) creates a 0-by-0 struct array.
struct(S) copies an existing scalar struct or struct array. Numeric fields retain exact class, shape, and storage. Construction does not gather resident values or otherwise transform field payloads.
GPU execution
Struct assembly is host bookkeeping, not a provider operation. A gpuArray field remains an owned resident handle inside the result; no kernel or implicit download occurs.
Examples
Create a scalar struct with named fields
s = struct("name", "entry", "score", 42)Expected output:
s has fields name and scoreBuild a struct array from cell-valued fields
names = {"first", "second"};
ages = {36, 45};
records = struct("name", names, "age", ages)Expected output:
records is a 1-by-2 struct arrayReplicate a scalar field across a struct array
ids = struct("id", {101, 102, 103}, "department", "Research")Expected output:
ids is a 1-by-3 struct arrayCopy an existing struct
a = struct("id", 7, "label", "demo");
b = struct(a);
b.id = 8Expected output:
a.id is 7 and b.id is 8Create an empty struct array
s = struct([]);
shape = size(s)Expected output:
shape = [0 0]Keep a gpuArray field resident
G = gpuArray(uint16([1 2; 3 4]));
s = struct("data", G, "label", "resident");
host = gather(s.data)Expected output:
host remains a uint16 matrixUsing struct with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how struct changes the result.
Run a small struct example, explain the result, then change one input and compare the output.
FAQ
Which field names are valid?⌄
Names must satisfy MATLAB identifier rules: start with a letter, contain only letters, digits, or underscores, fit within namelengthmax, and not be a keyword.
How do I create a struct array?⌄
Use equally shaped cell arrays as field values. Non-cell field values are replicated across every result element.
What happens when a field name repeats?⌄
Duplicate field names are rejected with RunMat:struct:DuplicateField.
What does struct([]) return?⌄
It returns a 0-by-0 struct array.
Does struct copy an existing struct?⌄
Yes. struct(S) returns an independent copy of a supported struct or struct array.
Does construction gather GPU fields?⌄
No. Resident handles remain resident field values until another operation explicitly uses or gathers them.
Related Structs functions
fieldnames · getfield · isfield · orderfields · rmfield · setfield · structfun
Source you can read
Every RunMat function's source is public. Read exactly how struct is executed, line by line, in Rust.
- View the source for struct in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.