Pass Structures and Cell Arrays in C MEX File
Passing structures and cell arrays into MEX files is like passing any other data type,
except the data itself in the C Matrix API is of type mxArray. In
practice, mxGetField (for structures) and
mxGetCell (for cell arrays) return pointers of type
mxArray. You treat the pointers like any other pointers of type
mxArray. To pass the data contained in the
mxArray to a C routine, use an API function such as
mxGetData to access it.
This MEX file example uses the C Matrix API. For a C++ MEX file example using the
MATLAB Data API for C++, see phonebook.cpp. For information about creating MEX
files with this API, see C++ MEX Functions.
This example takes an m-by-n structure matrix as
input and returns a new 1-by-1 structure that
contains these fields:
Text input generates an
m-by-ncell arrayNumeric input (noncomplex, scalar values) generates an
m-by-nvector of numbers with the same class ID as the input, for exampleint,double, and so on.
To build this example, at the command prompt type:
mex phonebook.cTo see how this program works, create this structure:
friends(1).name = 'Jordan Robert'; friends(1).phone = 3386; friends(2).name = 'Mary Smith'; friends(2).phone = 3912; friends(3).name = 'Stacy Flora'; friends(3).phone = 3238; friends(4).name = 'Harry Alpert'; friends(4).phone = 3077;
Call the MEX file:
phonebook(friends)
ans =
name: {1x4 cell }
phone: [3386 3912 3238 3077]