struct — Create scalar structs or struct arrays from field/value inputs with MATLAB-compatible construction rules.
S = struct(...) creates scalar structs or struct arrays by pairing field names with values. It supports MATLAB-compatible name/value, struct-copy, and cell-expansion construction forms.
Syntax
S = struct()
S = struct(template)
S = struct(field, value, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
template | Any | Yes | — | Existing struct/struct-array template or empty array for struct([]). |
field | PropertyName | Yes | — | Field name. |
value | Any | Yes | — | Field value or cell array of field values. |
name_value_pairs | Any | Variadic | — | Additional field/value pairs. |
Returns
| Name | Type | Description |
|---|---|---|
S | Any | Scalar struct or struct array. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:struct:InvalidSingleInput | Single input is neither struct, struct-array cell, nor empty numeric/logical array. | struct: expected name/value pairs, an existing struct or struct array, or [] to create an empty struct array |
RunMat:struct:NameValuePairs | Name/value arguments are not supplied in complete pairs. | struct: expected name/value pairs |
RunMat:struct:CellSizeMismatch | Cell value inputs for struct-array construction do not share the same shape. | struct: cell inputs must have matching sizes |
RunMat:struct:SizeOverflow | Requested struct-array size exceeds platform limits. | struct: struct array size exceeds platform limits |
RunMat:struct:AssembleFailed | Internal struct-array assembly failed. | struct: failed to assemble struct array |
RunMat:struct:EmptyArrayFailed | Internal empty struct-array creation failed. | struct: failed to create empty struct array |
RunMat:struct:StructArrayContents | Single-argument struct-array cell input contains non-struct values. | struct: single argument cell input must contain structs |
RunMat:struct:StructArrayCopyFailed | Copying a single-argument struct-array cell input failed. | struct: failed to copy struct array |
RunMat:struct:FieldNameType | Field name is not a string scalar or 1xN character vector. | struct: field names must be strings or character vectors |
RunMat:struct:FieldNameScalar | Field name char/string-array input is not scalar. | struct: field names must be scalar string arrays or character vectors |
RunMat:struct:FieldNameCharVector | Character-array field name input is not a 1-by-N character vector. | struct: field names must be 1-by-N character vectors |
RunMat:struct:FieldNameEmpty | Field name is empty. | struct: field names must be nonempty |
RunMat:struct:FieldNameStartChar | Field name does not start with a letter or underscore. | struct: field names must begin with a letter or underscore |
RunMat:struct:FieldNameInvalidChar | Field name includes unsupported characters. | struct: invalid character in field name |
How struct works
- Field names must satisfy the MATLAB
isvarnamerules: they start with a letter or underscore and contain only letters, digits, or underscores. - The last occurrence of a repeated field name wins and overwrites earlier values.
- String scalars, character vectors, and single-element string arrays are accepted as field names.
struct()returns a scalar struct with no fields, whilestruct([])yields a0×0struct array.- When any value input is a cell array, every cell array input must share the same size. Non-cell inputs are replicated across every element of the resulting struct array.
- Passing an existing struct or struct array (
struct(S)) creates a deep copy; the original data is untouched.
Does RunMat run struct on the GPU?
struct performs all bookkeeping on the host. GPU-resident values—such as tensors created with gpuArray—are stored as-is inside the resulting struct or struct array. No kernels are launched and no data is implicitly gathered back to the CPU.
GPU memory and residency
Usually not. RunMat's planner keeps GPU values resident as long as downstream operations can profit from them. You can still seed GPU residency explicitly with gpuArray for MATLAB compatibility; the handles remain untouched inside the struct until another builtin decides to gather or operate on them.
Examples
Creating a simple structure for named fields
s = struct("name", "Ada", "score", 42);
disp(s.name);
disp(s.score)Expected output:
Ada
42Building a struct array from paired cell inputs
names = {"Ada", "Grace"};
ages = {36, 45};
people = struct("name", names, "age", ages);
{people.name}Expected output:
{'Ada'} {'Grace'}Broadcasting scalars across a struct array
ids = struct("id", {101, 102, 103}, "department", "Research");
{ids.department}Expected output:
{'Research'} {'Research'} {'Research'}Copying an existing structure
a = struct("id", 7, "label", "demo");
b = struct(a);
b.id = 8;
disp([a.id b.id])Expected output:
7 8Building an empty struct array
s = struct([]);
disp(size(s))Expected output:
0 0Using 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
Do field names have to be valid identifiers?⌄
Yes. RunMat mirrors MATLAB and requires names to satisfy isvarname. Names must begin with a letter or underscore and may contain letters, digits, and underscores.
How do I create a struct array?⌄
Provide one or more value arguments as cell arrays with identical sizes. Each cell contributes the value for the corresponding struct element. Non-cell values are replicated across all elements.
What happens when the same field name appears more than once?⌄
The last value wins; earlier values for the same field are overwritten.
Does struct gather GPU data back to the CPU?⌄
No. GPU tensors remain device-resident handles inside the resulting struct or struct array.
Can I pass non-string objects as field names?⌄
No. Field names must be provided as string scalars, character vectors, or single-element string arrays. Passing other types raises an error.
Related Structs functions
fieldnames · getfield · isfield · orderfields · rmfield · setfield
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. 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 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.