Convert a cell array to a structure array.
SA = cell2struct(CA, fields) SA = cell2struct(CA, fields, dim)
cell2struct(CA,fields) converts a cell array to a structure array. The size of the result is size(SA)(2:end), where nf is the number of fields. Field SA(i1,i2,...).f of the result contains cell CA{j,i1,i2,...}, where f is field field{j}. Argument fields contains the field names as strings.
With a third input argument, cell2struct(CA,fields,dim) picks fields of each element along dimension dim. The size of the result is the size of CA where dimension dim is removed.
SA = cell2struct({1, 'ab'; 2, 'cde'}, {'a', 'b'}); SA = cell2struct({1, 2; 'ab', 'cde'}, {'a', 'b'}, 2);
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 its argument obj is a structure or structure array, false otherwise.
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; s=setfield(s,str,v) has the same effect as s.(str)=v.
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
structarray, isstruct, isfield, rmfield, fieldnames, operator {}
Convert a structure array to a cell array.
CA = struct2cell(SA)
struct2cell(SA) converts a structure or structure array to a cell array. The size of the result is [nf,size(SA)], where nf is the number of fields. Cell CA{j,i1,i2,...} of the result contains field SA(i1,i2,...).f, where f is the j:th field.
SA = cell2struct({1, 'ab'; 2, 'cde'}, {'a', 'b'}); CA = struct2cell(SA);
Create a structure array.
SA = structarray(fieldname1, A1, fieldname2, A2, ...)
structarray builds a new structure array. 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 values as a cell array. All cell arrays must have the same size; the resulting structure array has the same size.
SA = structarray('a', {1,2;3,4}, 'b', {'a', 1:3; 'def', true});