CSC 370

Compiling Mex-files in MATLAB

 

Matlab provides library functions for a number of important operations, and many additional tasks may be accomplished by writing m-files from scratch. But there are certain tasks that cannot be accomplished efficiently using the resources available within Matlab itself. To address such cases, Matlab provides an interface to allow code written in other languages (Fortran, C, & Java) to be called from the Matlab environment. Programs written for this interface must accept internal Matlab data structures as input, and produce similar structures as output. They may then be compiled and called from the Matlab environment just like any other function.

Mex-Files

We will be working with mex-files written in C (actually, since many C compilers also understand C++, we may use some C++ features also). As an example, we will look at a mex-file for computing the 4-connected components of an image. (Since the Image Processing Toolbox was introduced to Matlab, this function has been made obsolete by bwlabel. But it will serve fine as an example.) Copy cc4.cpp into your working directory. Take a look at it, and notice how it is structured. There is no main() function. Instead, when the code is called from Matlab execution begins with mexFunction(). This function checks the arguments passed to it to make sure they are appropriate, allocates space for an answer, and computes theconnected component labels. Along the way, it makes reference to a number of specially defined functions for interfacing with the Matlab environment, identified by the fact that they begin with either mx or mex. These functions are defined in the header file mex.h, included at the top of the file.

Before you can use the new function, you have to compile it using the Matlab command mex. The first step is to configure mex so it knows what compiler to use. You can do this by typing mex -setup at the command prompt. It will search for all the compilers on your system. Matlab comes with a compiler called lcc, which you can use if you don't have any others installed. However, it isn't a very efficient compiler, so if you have either gcc or the Microsoft Visual C++ compiler, you should use one of those. Once you have set up the compiler, you can compile your file, by providing a full path name to it.

>> mex -setup
Please choose your compiler for building external interface (MEX) files: 
 
Would you like mex to locate installed compilers [y]/n? y
 
Select a compiler: 
[1] Digital Visual Fortran version 6.0 in C:\Program Files\Microsoft Visual Studio 
[2] Lcc C version 2.4 in C:\OTHER\MATLAB13\sys\lcc 
[3] Microsoft Visual C/C++ version 7.0 in C:\Program Files\Microsoft Visual Studio .NET 
[4] Microsoft Visual C/C++ version 6.0 in C:\Program Files\Microsoft Visual Studio 
 
[0] None 
 
Compiler: 3
 
Please verify your choices: 
 
Compiler: Microsoft Visual C/C++ 7.0 
Location: C:\Program Files\Microsoft Visual Studio .NET 
 
Are these correct?([y]/n): y
 
The default options file: 
"C:\Documents and Settings\nhowe\Application Data\MathWorks\MATLAB\R13\mexopts.bat" 
is being updated from C:\OTHER\MATLAB13\BIN\WIN32\mexopts\msvc70opts.bat... 
 
>> mex 'H:\Projects\Matlab\cc4.cpp'
>> bw = (rand(4) > .5);
>> cc4(bw)

Documentation

One final note: On Windows, mex-files compile into a .mexw32 file. You can put a .m file of the same name into the same directory, containing comments about the function in question. The help and lookfor commands will reference this .m file, while the .mexw32 file will be used for execution.