Instructables
Picture of Making a Matlab MEX file
Instructions on how to integrate compiled C code with Matlab. MEX stands for MATLAB Executable. MEX-files are dynamically linked subroutines produced from C or Fortran source code that, when compiled, can be run from within MATLAB in the same way as MATLAB M-files or built-in functions. The external interface functions provide functionality to transfer data between MEX-files and MATLAB, and the ability to call MATLAB functions from C or Fortran code.

Here I'll show you everything you need to quickly make a useful mex file.
what you'll need:

= a text editor

= matlab 6.1 or above (earlier versions can do mex files too but slightly different format)

= matlab has it's own c compiler that comes with it, but I used visual c++ compiler cause I had it.
 
Remove these adsRemove these ads by Signing Up

Step 1: The code

Picture of The code
download the file I attached: mextest1p0.cpp
cpp stands for c++

.........This is what the code does:.........
it takes a number that you send it from the matlab command prompt.
It prints "hello world" in matlab
it returns two arguments to matlab, a two element number array and a string
the first element of the returned number array is 1 + the number you sent it
the second element of the returned number array is 2 + the number you sent it

..........This is how to implement it:............
save the file to your computer
start matlab
navigate in matlab to the directory where the file is
then follow the compilation and execution instructions at the top of the .cpp file. I can't write these instructions here because instructables auto-edits the commands and makes them appear incorrectly.

In the picture below you can see the code working. Don't worry about the "command line warning", it doesn't seem to effect anything. Notice on the left side, the current directory contains the .cpp file. On the right you can see me compiling the code and then executing it from matlab.


Read through the file in your text editor (for instance notepad in windows), it is very well documented and self explanatory, you will be able to do almost anything you need by following the conventions in the code (i.e. copying and pasting it and making slight modifications)
#######################################################################
........NOTE........
because of some annoying auto-editing that instructables does, the code as seen on the next page WILL NOT WORK, you HAVE TO DOWNLOAD THE FILE I attached instead of copying and pasting this.
########################################################################


rte37593 years ago
AND, Thanks for the example!!!!!
rte37593 years ago
I get an error in matlab R2009a

Mex file entry point is missing. Please check the (case-sensitive)
spelling of mexFunction (for C MEX-files), or the (case-insensitive)
spelling of MEXFUNCTION (for FORTRAN MEX-files).

Solution is to add the word const in line 78 of mextest1p0.cpp

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])