Posts Tagged ‘mex’

Using Mex with matlab

August 5, 2011 Leave a comment

I like programming in Matlab. I think it makes life a lot less painful and helps me in getting my work done faster. As Kevin Murphy would say Remember the 80/20 rule (80% of the time is spent in 20% of the code). Hence 80% of your code should be in some high level language, like matlab or Ocaml or R or python. Your time is more valuable than the computer’s time. “. Anyway, this post isn’t about starting an age old debate on matlab. However, here are some links that might be helpful for understanding matlab and debating(if you so wish).

Kevin Murphy’s matlab tips
Kevin Murphy’s s programming language comparison, leaning on interpreted languages.
(You can tell I am a Kevin Murphy fan 🙂 )

I merely want to tackle the simpler issue of using matlab’s mex features. So what is mex ? Mex interfaces matlab with C/C++.  Matlab has mostly been built from C/C++ routines with a bit  of java thrown in here and there for managing stuff. Writing code with loops in matlab can be extremely slow. You can get away with some tricks to make your code faster and matlab friendly.  Nevertheless, there might still be some bits of loopy O(n3) that you can’t wish away. So in that case, it might make sense to write that piece of code in C/C++ and then interface it with matlab. This works regardless of what OS you are running and you can get significant speedup in performance. It also helps the use case wherein you are using borrowed code in C/C++, while the rest of your awesome code is in matlab. Mex, can solve these issues too. Wherein you can call the C/C++ as a routine and even vice versa.

Here is a useful tutorial for using mex with matlab. I really like this post. The author has presented a clear but useful tutorial for basic mex cases.
Here is another one.
Here is the matlab documentation for mex.

The rest of this post is dedicated to some aspects on mex that I did not find in other tutorials/blogs after limited surfing. I think these features are really cool and give a lot of added functionality to mex.

Let me start with some motivation. I have been dealing with following use case lately. I was trying to run some matlab code I wrote on some benchmark problem sets. The problems were written in a custom format and did not fall into some standard scheme. Luckily, they provided some C code for parsing their problem sets. This is a good use case for mex. Instead of me re-writing the parser in m-code, I could use their C code as is and pass the relevant parsed data back to matlab using mex magic. For this I needed to pass data into the C code (arguments, filenames) and read back from it(parsed structure). This can be done via the mexFunction construct.

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

There are also a list of other cool things that can be done using mex in matlab. You can find the whole list here. Some of the useful things:

  • Pass strings back and forth (helpful in string manipulation/ working with text data)
  • Passing any number of inputs and outputs
  • Passing structures and cell arrays. This is a really useful feature. I know you can pass structures as input, but I am not sure if it works the other way.
  • Manipulate multidimensional and sparse arrays
  • Using mexCallMATLAB for calling matlab from within C/C++ code. This feature is really useful

To see some sample code. you can execute the following command in your matlab prompt.

>> listdir([matlabroot '/extern/examples/refbook'])

Categories: matlab Tags: ,