3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Making a Matlab MEX file

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 1The code

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.
########################################################################


« Previous StepDownload PDFView All StepsNext Step »
2 comments
Sep 9, 2010. 10:12 PMrte3759 says:
AND, Thanks for the example!!!!!
Sep 9, 2010. 10:11 PMrte3759 says:
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[])



Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
81
Followers
40
Author:leevonk
www.leevonk.com