[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A.2.5 Structures with Mex-Files
The basic function to create a structure in a mex-file is
mxCreateStructMatrix
, which creates a structure array with a two
dimensional matrix, or mxCreateStructArray
.
mxArray *mxCreateStructArray (int ndims, int *dims, int num_keys, const char **keys); mxArray *mxCreateStructMatrix (int rows, int cols, int num_keys, const char **keys); |
Accessing the fields of the structure can then be performed with the
mxGetField
and mxSetField
or alternatively with the
mxGetFieldByNumber
and mxSetFieldByNumber
functions.
mxArray *mxGetField (const mxArray *ptr, mwIndex index, const char *key); mxArray *mxGetFieldByNumber (const mxArray *ptr, mwIndex index, int key_num); void mxSetField (mxArray *ptr, mwIndex index, const char *key, mxArray *val); void mxSetFieldByNumber (mxArray *ptr, mwIndex index, int key_num, mxArray *val); |
A difference between the oct-file interface to structures and the
mex-file version is that the functions to operate on structures in
mex-files directly include an index
over the elements of the
arrays of elements per field
. Whereas the oct-file structure
includes a Cell Array per field of the structure.
An example that demonstrates the use of structures in mex-file can be found in the file ‘mystruct.c’, as seen below
An example of the behavior of this function within Octave is then
a(1).f1 = "f11"; a(1).f2 = "f12"; a(2).f1 = "f21"; a(2).f2 = "f22"; b = mystruct(a) ⇒ field f1(0) = f11 field f1(1) = f21 field f2(0) = f12 field f2(1) = f22 b = { this = (, [1] = this1 [2] = this2 [3] = this3 [4] = this4 ,) that = (, [1] = that1 [2] = that2 [3] = that3 [4] = that4 ,) } |