MEX is the MATLAB compiler, which enables a user to design a MATLAB program which can call a function written in C, C++, FORTRAN77 or FORTRAN90, and to transmit information to and from such functions.
This facility means a user can design a program that combines features of multiple languages, or takes advantage of the fact that a useful function is already available in source code form. Also, a user can take advantage of the interactive and graphic features of MATLAB in combination with calculations carried out in a traditional language.
A particular reason to use MEX occurs when a highly efficient kernel or library has been written in a compilable language, but the user also needs the interactivity, graphics, or other features of MATLAB.
The C/C++/F77/F90 function can accept input, return values, and print data. However, the function needs to communicate with MATLAB according to rules defined by the MEX compiler. This means that things such as the data types of variables and the specific form of commands to print data must be handled according to these rules.
Once the user function has been created, it can be compiled by the command "mex filename". This will create compiled code that can be called directly by MATLAB. For instance, the HELLO example includes a short C function called "hello.c". Issuing the command "mex hello.c" invokes a C compiler that includes the necessary MATLAB libraries, and creates a compiled file called "hello.mexmaci64" (the extension indicates that I have compiled with MEX on a MACintosh computer using 64 bit integer addressing). MATLAB can invoke this compiled function by the command "hello".
In order to use MATLAB's MEX compiler, the following things must be true:
The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.
MATLAB_CALLS_C, MATLAB programs which illustrate how C functions can be written, compiled, and called from MATLAB using the MEX facility;
MATLAB_CALLS_F77, MATLAB programs which illustrate how FORTRAN77 functions can be written, compiled, and called from MATLAB using MATLAB's mex facility;
NSASM, a C library which computes the jacobian matrix associated with a finite element approximation of the incompressible steady (time independent) Navier-Stokes equations in 2D, and to store these as a sparse matrix, by Per-Olof Persson. It is designed to be compiled with MATLAB's mex compiler and called from a MATLAB main program.
HELLO is the standard "Hello, world!" example. It uses a function written in C.
You can go up one level to the MATLAB source codes.