List of fields of a structure.
fields = fieldnames(strct)
fieldnames(strct) returns the field names of structure strct as a list of strings.
fieldnames(struct('a', 1, 'b', 1:5)) {'a', 'b'}
struct, isfield, orderfields, rmfield
Value of a field in a structure.
value = getfield(strct, name)
getfield(strct,name) gets the value of field name in structure strct. It is an error if the field does not exist. getfield(s,'f') gives the same value as s.f. getfield is especially useful when the field name is not fixed, but is stored in a variable or is the result of an expression.
operator ., struct, setfield, rmfield
Test for the existence of a field in a structure.
b = isfield(strct, name)
isfield(strct, name) is true if the structure strct has a field whose name is the string name, false otherwise.
isfield(struct('a', 1:3, 'x', 'abc'), 'x') true isfield(struct('a', 1:3, 'x', 'abc'), 'X') false
Test for a structure object.
b = isstruct(obj)
isstruct(obj) is true if the object obj is a structure, false otherwise. Structures are lists whose at least one field has a name.
isstruct(struct('a', 123)) true isstruct({1, 2, 'x'}) false a.f = 3; isstruct(a) true
struct, isfield, isa, islist, ischar, isobject, islogical
Reorders the fields of a structure.
strctout = orderfields(strctin) strctout = orderfields(strctin, structref) strctout = orderfields(strctin, names) strctout = orderfields(strctin, perm) (strctout, perm) = orderfields(...)
With a single input argument, orderfields(strctin) reorders structure fields by sorting them by field names.
With two input arguments, orderfields reorders the fields of the first argument after the second argument. Second argument can be a permutation vector containing integers from 1 to length(strctin), another structure with the same field names, or a list of names. In the last cases, all the fields of the structure must be present in the second argument.
The (first) output argument is a structure with the same fields and the same value as the first input argument; the only difference is the field order. An optional second output argument is set to the permutation vector.
s = struct('a',123,'c',1:3,'b','123') s = a: 123 c: real 1x3 b: 'abcde' (t, p) = orderfields(s) t = a: 123 b: 'abcde' c: real 1x3 p = 1 3 2 t = orderfields(s, {'c', 'b', 'a'}) t = c: real 1x3 b: 'abcde' a: 123
Deletion of a field in a structure.
strctout = rmfield(strctin, name)
strctout=rmfield(strctin,name) makes a structure strctout with the same fields as strctin, except for field named name which is removed. If field name does not exist, strctout is the same as strctin.
x = rmfield(struct('a', 1:3, 'b', 'abc'), 'a'); fieldnames(x) b
struct, setfield, getfield, orderfields
Assignment to a field in a structure.
strctout = setfield(strctin, name, value)
strctout=setfield(strctin,name,value) makes a structure strctout with the same fields as strctin, except that field named name is added if it does not exist yet and is set to value. s=setfield(s,'f',v) has the same effect as s.f=v. setfield is especially useful when the field name is not fixed, but is stored in a variable or is the result of an expression.
operator ., struct, getfield, rmfield
Creation of a structure
strct = struct(fieldname1, value1, fieldname2, value2, ...)
struct builds a new structure. Input arguments are used by pairs to create the fields; for each pair, the first argument is the field name, provided as a string, and the second one is the field value.
x = struct('a', 1, 'b', 2:5); x.a 1 x.b 2 3 4 5