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.
mextest1p0.cpp2 KB
Remove these ads by
Signing UpStep 1The code
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.
########################################################################
mextest1p0.cpp2 KB| « Previous Step | Download PDFView All Steps | Next Step » |








































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[])