The main elements of mex files in Matlab, written in C / C + +, are mainly four
- header mex.h
- gateway mexFunction
- arrays mxArray
- APIs function
mex.h
# include mex.h
Each mex-file written in C / C + + must have a header of mex.h include directive, so you can use the API in the form of mx * routines
Gateway mexFunction
mexFunction (nlhs int, mxArray * plhs [], int nrhs, const mxArray * prhs
[]) {
...
}
The routine between the gateway and Matlab mex file is called mexFunction and, essentially, is the "main" library that we are going to implement.
This routine, whose prototype is declared in mex.h said, contains the following elements:
- nlhs mxArray is the number of outgoing (lhs - left hand side)
- plhs is an array of pointers to the mxArray expected outputs
- nrhs is the number input (rhs - right hand side)
- prhs is an array of pointers to the mxArray data input, which are read-only (const)
Note that the suffix of the names of these variables relates to their usual position in a function call into Matlab. When you start executing the code contained in the mex-file, the pointers in plhs are undefined, as it is for the program explicitly define them (the compiler will not check whether that is).
mxArray
This is a data structure that manages the data of the program and C is the representation of vectors / matrices / string / cell Matlab
A
mxArray is declared as any variable
mxArray * myarray;
but it must be initialized with a mx * routines before being used (eg, mxCreateNumericArray). Note that the data in an mxArray is read / written in the typical order of Matlab, or in the column to the right, unlike C / C + + that reads in a row and down.
The API functions
The API function include, among other things, the functionality to transfer data between MEX-files and Matlab, the ability to call Matlab functions from source code in C / C + + 's access to data contained in an mxArray. The most used are related to:
- creating arrays (mxCreateNumericArray, mxCreateCellArray, mxCreateCharArray)
- array access (mxGetPr, mxGetPi, mxGetM, mxGetM, mxGetData, mxGetCell)
- changes to the array (mxSetPr, mxSetPi, mxSetData, mxSetField)
- control of an array (mxIsClass)
- memory management (mxMalloc, mxCalloc, mxFree, mexMakeMemoryPersistent, mexAtExit, mxDestroyArray, memcpy)
- other (mexEvalString, mexCallMATLAB, mexPrintf , mexWarnMsgTxt)
A description complete can be found in MATLAB External / API Reference Guide .
Here we present an example of a MEX-file to create an mxArray to hold the output data
# include "mex.h"
mexFunction void (int nlhs, mxArray * plhs [], int
nrhs, const mxArray * prhs []) {
int i, j, m, n;
double * data1, * data2;
if (nrhs! = nlhs)
mexErrMsgTxt ("The number of input and output arguments must be the same. ");
for (i = 0; i \u0026lt;nrhs; i + +)
{
/ * Find the dimensions of the data * /
m = mxGetM (prhs [i]);
n = mxGetN (prhs [i ]);
/ * Create an mxArray for the output data * /
plhs [i] = mxCreateDoubleMatrix (m, n, mxREAL)
/ * Retrieve the input data * / data1 =
mxGetPr (prhs [i]);
/ * Create a pointer to the output data * /
data2 = mxGetPr (plhs [i]);
/ * Put data in the output array * /
for (j = 0, j \u0026lt;m * n, j + + )
{data2 [j] = 2 * data1 [j];
}}}
Other examples can be found here .