In this tutorial I will show you a very simple example of a MEX file in MATLAB. A MEX file is a compiled version of a C/C++ code written and compiled in MATLAB that can be used/executed as an M-file. A MEX file has a way better performance respect to a m-file in MATLAB. Thus if you want to increase the performance of your code, converting it into a MEX file is one of the ways.

1 – To create your first experience open MATLAB and create a multiplier.c file in the workspace.

2- The first thing you should add to your code is header files. The most useful one in MATLAB is mex.h, so add it to your code using the include command:

#include "mex.h"

3- In this example and generally in any code you want to write you will have some input/output for the program and you have to check and pass them into the program. When using MEX files, you can write a gateway function for this purposes:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *inVector1; /* input matrix */
double *inVector2; /* 1xN input matrix */
size_t ncols; /* size of matrix */
double *product; /* output scalar */
/* create a pointer to the real data in the input matrix */
inVector1 = mxGetPr(prhs[0]);
inVector2 = mxGetPr(prhs[1]);
/* get dimensions of the input matrix */
ncols = mxGetN(prhs[0]);
/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
/* get a pointer to the real data in the output matrix */
product = mxGetPr(plhs[0]);
/* call the computational routine */
dotProduct(inVector1, inVector2, product, (mwSize)ncols);
}

4- The main function that performs the main calculation, which is a dot product of two vectors in our example can be written as follows:

void dotProduct(double *M1, double *M2, double *M3, mwSize n)
{
mwSize i;
for (i=0;i<n;i++)
{
M3[0] += M1[i] * M2[i];
}
}

Here we have used mwSize instead of int which allows us to have cross-platform code.

5- Now, you can compile the code in MATLAB

mex multiplier.c

6- Now you can test the function in MATLAB. Remember that a MEX file can be directly executed in MATLAB:

[sourceode langaue=”matlab”]

A = [1 2 3];

B = [3 4 5];

multiplier(A,B)

ans =

26

[/sourcecode]

The whole sourcecode can be found as follows:

/*
* this is a very basic example of a matrix multiplier
*
*/
#include "mex.h"
void dotProduct(double *M1, double *M2, double *M3, mwSize n)
{
mwSize i;
for (i=0;i<n;i++)
{
M3[0] += M1[i] * M2[i];
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *inVector1; /* input scalar */
double *inVector2; /* 1xN input matrix */
size_t ncols; /* size of matrix */
double *product; /* output matrix */
/* create a pointer to the real data in the input matrix */
inVector1 = mxGetPr(prhs[0]);
inVector2 = mxGetPr(prhs[1]);
/* get dimensions of the input matrix */
ncols = mxGetN(prhs[0]);
/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
/* get a pointer to the real data in the output matrix */
product = mxGetPr(plhs[0]);
/* call the computational routine */
dotProduct(inVector1, inVector2, product, (mwSize)ncols);
}