en fr

Disponible uniquement en anglais

Sysquake Pro – Table of Contents

Sysquake – Table of Contents

Sysquake for LaTeX – Table of Contents

Structure Functions

cell2struct

Convert a cell array to a structure array.

Syntax

SA = cell2struct(CA, fields)
SA = cell2struct(CA, fields, dim)

Description

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.

Examples

SA = cell2struct({1, 'ab'; 2, 'cde'}, {'a', 'b'});
SA = cell2struct({1, 2; 'ab', 'cde'}, {'a', 'b'}, 2);

See also

struct2cell

fieldnames

List of fields of a structure.

Syntax

fields = fieldnames(strct)

Description

fieldnames(strct) returns the field names of structure strct as a list of strings.

Example

fieldnames({a=1, b=1:5})
  {'a', 'b'}

See also

struct, isfield, orderfields, rmfield

getfield

Value of a field in a structure.

Syntax

value = getfield(strct, name)

Description

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.

See also

operator ., struct, setfield, rmfield

isfield

Test for the existence of a field in a structure.

Syntax

b = isfield(strct, name)

Description

isfield(strct, name) is true if the structure strct has a field whose name is the string name, false otherwise.

Examples

isfield({a=1:3, x='abc'}, 'a')
  true
isfield({a=1:3, x='abc'}, 'A')
  false

See also

fieldnames, isstruct, struct

isstruct

Test for a structure object.

Syntax

b = isstruct(obj)

Description

isstruct(obj) is true if its argument obj is a structure or structure array, false otherwise.

Examples

isstruct({a=123})
  true
isstruct({1, 2, 'x'})
  false
a.f = 3;
isstruct(a)
  true

See also

struct, isfield, isa, islist, ischar, isobject, islogical

orderfields

Reorders the fields of a structure.

Syntax

strctout = orderfields(strctin)
strctout = orderfields(strctin, structref)
strctout = orderfields(strctin, names)
strctout = orderfields(strctin, perm)
(strctout, perm) = orderfields(...)

Description

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.

Examples

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

See also

struct, fieldnames

rmfield

Deletion of a field in a structure.

Syntax

strctout = rmfield(strctin, name)

Description

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.

Example

x = rmfield({a=1:3, b='abc'}, 'a');
fieldnames(x)
  b

See also

struct, setfield, getfield, orderfields

setfield

Assignment to a field in a structure.

Syntax

strctout = setfield(strctin, name, value)

Description

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.

See also

operator ., struct, getfield, rmfield

struct

Creation of a structure

Syntax

strct = struct(field1=value1, field2=value2, ...)
strct = struct(fieldname1, value1, fieldname2, value2, ...)
strct = {field1=value1, field2=value2, ...}

Description

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.

Examples

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

See also

structarray, isstruct, isfield, rmfield, fieldnames, operator {}

struct2cell

Convert a structure array to a cell array.

Syntax

CA = struct2cell(SA)

Description

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.

Example

SA = cell2struct({1, 'ab'; 2, 'cde'}, {'a', 'b'});
CA = struct2cell(SA);

See also

cell2struct

structarray

Create a structure array.

Syntax

SA = structarray(field1=A1, field2=A2, ...)
SA = structarray(fieldname1, A1, fieldname2, A2, ...)

Description

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.

Example

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});

See also

struct, cell2struct

structmerge

Merge the fields of two structures.

Syntax

S = structmerge(S1, S2)

Description

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.

Examples

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

See also

fieldnames, setfield, cat