Skip to content
Jonathan H edited this page Oct 31, 2016 · 22 revisions

Structures

Matlab structures are data containers with named fields. The methods of this submodule allow you to define, modify and transform these structures.


dk.struct.repeat

An adaptation of repmat for structures. Convenient to allocate empty struct-arrays with predefined fields.

Signature:

    s = repeat( fields, varargin )

Example:

dk.struct.repeat( {}, [2,3,4] ) % 2x3x4 struct-array with no field
dk.struct.repeat( {'single'}, [1,2] ) % fields must be a cell, even for one field
dk.struct.repeat( {'field1','field2'}, [2,3] ) % multiple fields without repeat

dk.struct.set|get|rem

These methods apply to both scalar strutures and struct-arrays.

Signatures:

    s = dk.struct.set( s, field, value, overwrite=false )
    v = dk.struct.get( s, field, default )
    s = dk.struct.rem( s, varargin )

Example:

% 2x3 struct-array with single field 'a'
s = dk.struct.repeat( {'a'}, [2 3] )

% field 'b' is added with values 0
s = dk.struct.set( s, 'b', 0 ); {s.b}

% field 'b' already exists, so this won't do anything
t = dk.struct.set( s, 'b', 10:10:60 ); {t.b}

% we need to authorise overwrite
t = dk.struct.set( s, 'b', 10:10:60, true ); {t.b}

% field 'c' is undefined
v = dk.struct.get( t, 'c', [0,1] ) % 2x3 cell repeating default value [0,1]
v = dk.struct.get( t, 'c' ) % throws an error

% field 'b' exists
v = dk.struct.get( t, 'b' ) % 2x3 cell with field values

% remove field 'a', but don't throw an error about 'c' being undefined
u = dk.struct.rem( t, 'c', 'a' )

dk.struct.to_cell

Text goes here

dk.struct.to_vars

Text goes here

dk.struct.fields|values

Text goes here

dk.struct.grid|array

Text goes here

dk.struct.merge

Text goes here

Clone this wiki locally