Keith Neo Kian Seng

main page
machine learning | gaussian process | source codes | additional stuffs
publication lists | Systems Modelling Group publications
personal interests | my computer
contact page | location map
more about myself | guest book
other links

Instructions To Compile MATLAB Mex-C Codes

small logo

Compilation of MATLAB Mex-C Source Codes

There are some tweaks and tricks which I have learnt during my encounter while doing my research. Below are some of them.

Instructions To Compile MATLAB Mex-C Codes

Writing Mex-C codes is very similar to writing any C codes, if you understand C programming. I will offer here a very basic and simple solution to build a Mex-C code, and compile it so that you can call the C code directly from MATLAB. No advanced knowledge of C is required, though a minumum of C programming is still necessary.

It is always easier to explain stuffs with an example. So, I'll start by trying to program the following code in C, and call the code from MATLAB environment.

Example: z = x + y
such that x and y are inputs, and z is the output we want to obtain from the C code.

Let give the function a name, i.e. sum_z. You will expect sum_z(x,y) to be the function calling the C code, returning a scalar value. The following is the syntax for the creating a mex-c source code:

/* you will need to include the following line for all mex-c source code */
#include "mex.h"

/* enter your usual C source code here */
void sum_z(double *a,double *b,double *c)
{
c[0] = a[0]+b[0];
return;
}

/* end of C source code */

/* function calling linkage component to MATLAB */
/* DO NOT edit the next 2 lines */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{

/* define input, output and any variables */
double *Z;
double *X;
double *Y;

/* define any error message or criterion here */
if(nrhs!=2) {
mexErrMsgTxt("Two inputs required.");
} else if(nlhs>1) {
mexErrMsgTxt("Too many output arguments");
}
/* end of error message or criterion */

/* define memory allocation in MATLAB for the outputs*/
plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);

X = mxGetPr(prhs[0]);
Y = mxGetPr(prhs[1]);
Z = mxGetPr(plhs[0]);

sum_z(X,Y,Z);
return;
}

Once you have written your C code, save it as sum_xy.c in one of your folder, accessible by Matlab. Next, run Matlab program, and go to the directory (or folder) where the C code file is saved. Run the following command:

>> mex -c -v sum_xy.c

Press enter, and wait for Matlab to compile the C code for you. Depending on your system platform, Matlab will create a .dll (Windows) or .mexglx (Linux/UNIX) file in the same folder, with the same filename (not file extension). Test out your Mex-C code by entering the following operation, i.e. sum of 103 and 43:

>> sum_xy(103,43)

You should obtain 146 as your answer. Simple!

Email or contact me, if you would like to know more. I welcome any further discussion.

Java implementation in MATLAB is also possible, especially for programmers who like to work in JAVA. The JAVA-MATLAB implementation solution is offered by my colleague, Dr. Yunong Zhang.

About Me | Site Map | Private Policy | Contact Me | ©2008 Keith Neo