Structure Functions

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(struct('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(struct('a', 1:3, 'x', 'abc'), 'x')
  true
isfield(struct('a', 1:3, 'x', 'abc'), 'X')
  false

See also

isstruct, struct

isstruct

Test for a structure object.

Syntax

b = isstruct(obj)

Description

isstruct(obj) is true if the object obj is a structure, false otherwise. Structures are lists whose at least one field has a name.

Examples

isstruct(struct('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 = 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

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(struct('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. setfield 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, getfield, rmfield

struct

Creation of a structure

Syntax

strct = struct(fieldname1, value1, fieldname2, value2, ...)

Description

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.

Example

x = struct('a', 1, 'b', 2:5);
x.a
  1
x.b
  2 3 4 5

See also

isstruct, isfield, rmfield, fieldnames, operator {}


Copyright 1998-2007, Calerga.
All rights reserved.