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

Structures

Matlab structures are associative data containers mapping a field name (string) to a value (any). The methods of this submodule allow you to define, modify and transform these structures.

Function Description
dk.struct.repeat An adaptation of repmat for structures
[`dk.struct.grid array`](#grid)
[`dk.struct.set get
[`dk.struct.fields values`](#fieldval)
dk.struct.merge Merge two structures (recursively or not)
dk.struct.to_cell What I would expect struct2cell to do
dk.struct.to_vars Turn the fields of a structure to variables in the calling scope

dk.struct.repeat

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

Signature:

    s = dk.struct.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 a single field
dk.struct.repeat( {'field1','field2'}, [2,3] ) % multiple fields without repeat

dk.struct.grid|array

Define a struct-matrix


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.fields|values

Extract the fields and values from a scalar structure or struct-array.

Signatures:

    f = dk.struct.fields( s )
    v = dk.struct.values( s )

Example:

% create 2x3 struct-array
s = dk.struct.array( 'a', 1:6, 'b', 10:10:60 );
s = reshape( s, [2,3] );

% fields is just an alias for fieldnames
f = dk.struct.fields(s)

% values returns a NxF cell-array where
%   N is the number of structures
%   F is the number of fields
v = dk.struct.values(s)

dk.struct.merge

Text goes here


dk.struct.to_cell

Does what I would expect struct2cell to do; it builds a cell { field1, value1, field2, ... } from a scalar structure struct( 'field1', value1, 'field2', ... ). Note that this function only supports scalar structures (no struct-array).

Signature:

    c = dk.struct.to_cell( s, recursive=false )

Example:

s = struct( 'a', 1, 'b', struct('aa',[],'bb',0) )
dk.struct.to_cell(s) % non-recursive by default
dk.struct.to_cell(s,true)

dk.struct.to_vars

Define the fields of a named structure as variables in the calling function (or console).

Signature:

    varargout = dk.struct.to_vars( s, varname_handle )

Example:

clear foo bar
s = struct('foo',1,'bar','baz');
dk.struct.to_vars(s); {foo,bar}

% If an output is required, a string is returned with the commands to define each variable, but it is not evaluated.
% The additional function handle allows to edit variable names.
cmd = dk.struct.to_vars( s, @(x) ['tmp_' x] ) % 'tmp_foo=s.foo; tmp_bar=s.bar;'
Clone this wiki locally