struct
- :
s =struct()¶ - :
s =struct(field1, value1, field2, value2, …)¶ - :
s =struct(obj)¶ -
Build a structure and fill in its fields.
Each field is a string giving a field name and the following value supplies that field’s contents, which may be of any type. With no arguments at all, the result is a 1-by-1 structure that has no fields.
When a value is a cell array the result becomes a structure array rather than a scalar structure: the cell is spread across the elements of the array, one entry per element. Every non-scalar cell value must share the same dimensions, and those dimensions set the shape of the array. A value that is not a cell, or is a one-element cell, is copied into every element. If the cell values are empty, the result is an empty structure array that still carries the named fields.
Two special single-argument forms are also recognized: given an existing structure the same structure is returned, and given a class object the plain structure underneath it is returned. Passing an empty numeric matrix (on its own, or followed by a cell array of field names) yields an empty structure array whose shape matches that matrix.
Observe that the syntax is optimized for struct arrays. Consider the following examples:
struct ("foo", 1) ⇒ scalar structure containing the fields: foo = 1 struct ("foo", {}) ⇒ 0x0 struct array containing the fields: foo struct ("foo", { {} }) ⇒ scalar structure containing the fields: foo = {}(0x0) struct ("foo", {1, 2, 3}) ⇒ 1x3 struct array containing the fields: fooIn the first line a plain value gives a scalar struct with one field. In the second, an empty cell as the value yields an empty struct array that still declares the field but holds no elements. In the third, a cell holding exactly one item collapses to a scalar struct whose field is set to that item—which here is itself an empty cell. In the last, a multi-element cell spreads across a struct array of the cell’s shape.