Pass Structures and Cell Arrays in C MEX File - MATLAB & Simulink (original) (raw)
Main Content
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) andmxGetCell
(for cell arrays) return pointers of typemxArray
. You treat the pointers like any other pointers of typemxArray
. To pass the data contained in themxArray
to a C routine, use an API function such asmxGetData
to access it.
This MEX file example uses the C Matrix API. For a C++ MEX file example using theMATLAB 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-n
cell array - Numeric input (noncomplex, scalar values) generates an
m
-by-n
vector 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:
To 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:
ans = name: {1x4 cell } phone: [3386 3912 3238 3077]