struct
Structure array
Description
A structure array is a data type that groups related data
using data containers called fields. Each field can contain any
type of data. Access data in a field using dot notation of the form
structName.fieldName.
Creation
When you have data to put into a new structure, create the structure using dot notation to name its fields one at a time:
s.a = 1;
s.b = {'A','B','C'}s = struct with fields:
a: 1
b: {'A' 'B' 'C'}
Field names can contain ASCII letters (A–Z, a–z), digits (0–9), and underscores, and
must begin with a letter. The maximum length of a field name is
namelengthmax.
You also can create a structure array using the struct function,
described below. You can specify many fields simultaneously, or create a nonscalar
structure array.
Syntax
Description
s = struct creates a scalar (1-by-1) structure with
no fields.
s = struct(
creates a structure array with the specified field and value. The
field,value)value input argument can be any data type, such as a
numeric, logical, character, or cell array.
If
valueis not a cell array, or ifvalueis a scalar cell array, thensis a scalar structure. For instance,s = struct('a',[1 2 3])creates a 1-by-1 structure, wheres.a = [1 2 3].If
valueis a nonscalar cell array, thensis a structure array with the same dimensions asvalue. Each element ofscontains the corresponding element ofvalue. For example,s = struct('x',{'a','b'})returnss(1).x = 'a'ands(2).x = 'b'.If
valueis an empty cell array{}, thensis an empty (0-by-0) structure.
s = struct(field1,value1,...,fieldN,valueN) creates a
structure array with multiple fields.
If none of the
valueinputs are cell arrays, or if allvalueinputs that are cell arrays are scalars, thensis a scalar structure.If any of the
valueinputs is a nonscalar cell array, thenshas the same dimensions as that cell array. Also, if two or morevalueinputs are nonscalar cell arrays, then they all must have the same dimensions.For any
valuethat is a scalar cell array or an array of any other data type,structinserts the contents ofvaluein the relevant field for all elements ofs. For example,s = struct('x',{'a','b'},'y','c')returnss(1).x = 'a',s(2).x = 'b',s(1).y = 'c', ands(2).y = 'c'.If any
valueinput is an empty cell array,{}, then outputsis an empty (0-by-0) structure. To specify an empty field and keep the values of the other fields, use[]as avalueinput instead.
s = struct([]) creates an empty (0-by-0) structure
with no fields.
s = struct( creates a scalar
structure with field names and values that correspond to properties of
obj)obj. The struct function does not
convert obj, but rather creates s as a
new structure. This structure does not retain the class information, so
private, protected, and hidden properties become public fields in
s. The struct function issues a
warning when you use this syntax.
