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({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({a=1:3, x='abc'}, 'a') true isfield({a=1:3, x='abc'}, 'A') 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({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 = {a=123, c=1:3, b='abcde'} 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({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(field1=value1, field2=value2, ...) strct = struct(fieldname1, value1, fieldname2, value2, ...) strct = {field1=value1, field2=value2, ...}
struct builds a new structure. With named arguments, the name of each argument is used as the field name. Otherwise, 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.
Instead of named arguments, a more compact notation consists in writing named values between braces. In that case, all values must be named; when no value has a name, a list is created, and mixed named and unnamed values are invalid. Fields are separated by commas; semicolons separate elements of n-by-1 struct arrays. See the documentation of braces for more details.
Three equivalent ways to create a structure with two fields a and b:
x = {a=1, b=2:5}; x = struct(a=1, b=2:5); 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(field1=A1, field2=A2, ...) SA = structarray(fieldname1, A1, fieldname2, A2, ...)
structarray builds a new structure array. With named arguments, the name of each argument is used as the field name, and the value is a cell array whose elements become the corresponding values in the result. Otherwise, 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.
In both cases, all cell arrays must have the same size; the resulting structure array has the same size.
The following assignments produce the same result:
SA = structarray(a = {1,2;3,4}, b = {'a', 1:3; 'def', true}); SA = structarray('a', {1,2;3,4}, 'b', {'a', 1:3; 'def', true});
Merge the fields of two structures.
S = structmerge(S1, S2)
structmerge(S1,S2) merges the fields of S1 and S2, producing a new structure containing the fields of both input arguments. More precisely, to build the result, structmerge starts with S1; each field which also exists in S2 is set to the value in S2; and finally, fields in S2 which do not exist in S1 are added.
If S1 and/or S2 are structure arrays, they must have the same size or one of them must be a simple structure (size 1x1). The result is a structure array of the same size where each element is obtained separately from the corresponding elements of S1 and S2; a simple structure argument is reused as necessary.
S = structmerge({a=2}, {b=3}) S = a: 2 b: 3 S = structmerge({a=1:3, b=4}, {a='AB', c=10}) S = a: 'AB' b: 4 c: 10