| MATLAB Function Reference | ![]() |
s = struct('field1', values1, 'field2',
values2, ...)
s = struct('field1', {}, 'field2',
{}, ...)
s = struct
s = struct([])
s = struct(obj)
s = struct('field1', values1, 'field2', values2, ...) creates a structure array with the specified fields and values. Each value input (values1, values2, etc.), can either be a cell array or a scalar value. Those that are cell arrays must all have the same dimensions.
The size of the resulting structure is the same size as the value cell arrays, or 1-by-1 if none of the values is a cell array. Elements of the value array inputs are placed into corresponding structure array elements.
Note If any of the values fields is an empty cell array {}, the MATLAB® software creates an empty structure array in which all fields are also empty. |
Structure field names must begin with a letter, and are case-sensitive. The rest of the name may contain letters, numerals, and underscore characters. Use the namelengthmax function to determine the maximum length of a field name.
s = struct('field1', {}, 'field2', {}, ...) creates an empty structure with fields field1, field2, ...
s = struct creates a 1-by-1 structure with no fields.
s = struct([]) creates an empty structure with no fields.
s = struct(obj) creates a structure s that is identical to the underlying structure in the input object obj. MATLAB does not convert obj, but rather creates s as a new structure. This structure does not retain the class information in obj.
The most common way to access the data in a structure is by specifying the name of the field that you want to reference. Another means of accessing structure data is to use dynamic field names. These names express the field as a variable expression that MATLAB evaluates at run-time.
To create fields that contain cell arrays, place the cell arrays within a value cell array. For instance, to create a 1-by-1 structure, type
s = struct('strings',{{'hello','yes'}},'lengths',[5 3])
s =
strings: {'hello' 'yes'}
lengths: [5 3]When using the syntax
s = struct('field1', values1, 'field2', values2, ...)the values inputs can be cell arrays or scalar values. For those values that are specified as a cell array, MATLAB assigns each element of values{m,n,...} to the corresponding field in each element of structure s:
s(m,n,...).fieldN = valuesN{m,n,...}For those values that are scalar, MATLAB assigns that single value to the corresponding field for all elements of structure s:
s(m,n,...).fieldN = valuesN
See Example 3, below.
The command
s = struct('type', {'big','little'}, 'color', {'red'}, ...
'x', {3 4})produces a structure array s:
s =
1x2 struct array with fields:
type
color
xThe value arrays have been distributed among the fields of s:
s(1)
ans =
type: 'big'
color: 'red'
x: 3
s(2)
ans =
type: 'little'
color: 'red'
x: 4Similarly, the command
a.b = struct('z', {});produces an empty structure a.b with field z.
a.b
ans =
0x0 struct array with fields:
zThis example initializes one field f1 using a cell array, and the other f2 using a scalar value:
s = struct('f1', {1 3; 2 4}, 'f2', 25)
s =
2x2 struct array with fields:
f1
f2Field f1 in each element of s is assigned the corresponding value from the cell array {1 3; 2 4}:
s.f1
ans =
1
ans =
2
ans =
3
ans =
4Field f2 for all elements of s is assigned one common value because the values input for this field was specified as a scalar:
s.f2
ans =
25
ans =
25
ans =
25
ans =
25isstruct, fieldnames, isfield, orderfields, getfield, setfield, rmfield, substruct, deal, cell2struct, struct2cell, namelengthmax, dynamic field names
![]() | strtrim | struct2cell | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |