-
Notifications
You must be signed in to change notification settings - Fork 0
struct
Jonathan H edited this page Oct 31, 2016
·
22 revisions
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 equivalent ofrepmat
for structures -
dk.struct.set|get|rem
Manipulate the fields of a structure -
dk.struct.to_cell
What you would expectstruct2cell
to do -
dk.struct.to_vars
Turn the fields of a structure to variables in the calling scope -
dk.struct.fields|values
Get the fields/values of a structure -
dk.struct.grid|array
Create struct-arrays with specified fields -
dk.struct.merge
Merge two structures (recursively or not)
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
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' )
Text goes here
Text goes here
Text goes here
Text goes here
Text goes here