What does the struct function do in MATLAB / RunMat?
S = struct(...) creates scalar structs or struct arrays by pairing field names with values. The
inputs can be simple name/value pairs, existing structs, or cell arrays whose elements are expanded
into struct array entries.
How does the struct function behave in MATLAB / RunMat?
- 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.
struct Function GPU Execution Behaviour
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 residency in RunMat (Do I need gpuArray?)
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
42
Building 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 8
Building an empty struct array
s = struct([]);
disp(size(s));
Expected output:
0 0
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.