Matlab is  a great tool for simulating and analyzing data. Its quite easy to write a script, that will do whatever you want since matlab has build-in functions for almost anything. However everything comes with cons as well. If you are doing some extra hard work with matlab, you may notice its quite slow, especially when it comes to for-cycles. Furthermore, If you are developing an algorithm, or testing the functionality of your C++ code, matlab is a great companion, because you can easily visualize your data with one or two commands.

Fortunately there is a way you can call your externall c++ code directly from matlab! You just have to create “a mex file”. To be more specific, you need to build a file “someting.mexw32” or “something.mexw64“.  Of course,if you are building a mex file, make sure to choose correct version. You cant run .mex64 from 32-bit matlab and .mexw32 from 64-bit matlab. We will start from beginning:

  • Open your Visual Studio 2013 and create a new Visual C++ Win32 project.
  • Select Console Application and check “empty project”
  • Under Source Files, add new Source.cpp
Now comes the funny part. Open your Project Settings.
  • Click General – Target Extension and change it to: ” .mexw32
  • Change Configuration Type to: ” Dynamic Library (.dll)
  • C/C++  ->  General -> Additional Include Directories:  ” … Matlab2014A32Bit\extern\include
  • Linker -> General -> Additional Library Directories: ” … Matlab2014A32Bit\extern\lib\win32\microsoft
  • Linker -> Input -> Additional Dependecies:  Add “libmat.lib“, “libmex.lib” and “libmx.lib
  • Linker -> Command Line -> Additional Options: “/export:mexFunction

Now we can finally proceed to some code. Each mexfile has a function “MexFunction” visible only to matlab, however you can have as many other functions as you want in the source file. feel free to include any files you want. You can of course use other libraries as well. I will demonstrate the usage of a mexfile on a simple example,t hat will consist of adding 13 to a matlab input Matrix. But before I post anything, please note, that matlab uses column-major order for storing multidimensional arrays. Furthermore, Matlab primarly uses double precision (Thats why is it so slow :D ).



As you can see, Its quite easy to create a MEX File. I believe the code should be self-explananotry with the comments provided. To sum it up, we obtain an mxArray variable from matlab, create a local instance of the array, then we derive its dimensions (Nx,Ny – Note Ny is at position = 0), get the standard 1D C++ Double pointer and as I mentioned before, we do a Column-to-Row operation. Then we are free to do whatever we want (Add 13 :D). After we are finished, we define output as mxArray, get its 1D C++ Double Pointer and do a Row-to-Column Operation. All you have to do now is to call this function from matlab (MatlabSimpleMex was the name of my project):


  •  The “clear mex” command enables you to rebuild your mex file from Visual Studio without closing matlab.
  • Use ” mexPrintf() ” function – similar to printf() – to write to matlab command window.
  • If you are willing to create higher dimensional output than 2D, use the  “mxCreateNumericArray()
  • Use mxGetPr() and mxGetPi() for Real and Imaginary Data Pointers.
  • If you wonder,why I am adding 13, check google for “Taylor Swift’s favorite number ” :)