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: ,

Google Nexus S

August 4, 2011 1 comment

I bought a new phone. The Google Nexus S. I had been waiting for a while for my current contract to end so that I could get a new phone. Though now I realize that there was no need to wait. I could have gotten an upgrade a while back. But lo and behold, my wait did not prove too fruitless. The Google Nexus S phone was on a deal today. They say only for today, but who knows.

So why did I choose to jump at this opportunity and buy it?  Well this phone certainly has some really good features. This will also be my first android phone and Gingerbread 2.3 looks promising.  I will now present some of the features/criticisms of this phone as best as I understand it :

Features:

  • It’s a sleek looking phone and similar to the other Samsung galaxy phones.
  • The UI is supposed to be slightly better than the nexus one. It has a new copy/paste word option.
  • Extended battery life. Gingerbread is better at policing power hungry apps.
  • The Gingerbread OS is also supposed to be a lot faster. Latency is virtually non-existent.
  • The shape of the phone is supposed to be ever so slightly curved, which makes for better ergonomics.
  • The camera is supposed to be stellar as well. Offering bright flash and clean images.
  • Haptic vibration feedback  (common in other smartphones)
  • The original Google experience ..! Android 2.3 is popular in other models as well.

Criticisms:

  • Apparently it is not compatible with T-mobile’s high speed networks. So its not really 4G. Wonder if they’ve fixed that now with the other service providers.
  • Though it has 16gb of space, it does not have a microSD slot.

Welcome to TechnObservations

August 3, 2011 Leave a comment

Dear Reader, Welcome to the TechnObservations blog. This blog is written and maintained by Subhodeep Moitra. I write some other blogs such as this one and this one too. While the blog has no specific purpose at this stage, there are hopes that it might evolve into something better. But mostly, it is my humble opinion supported by the limits of my knowledge that fill up the posts of this tech blog. Happy reading.

Categories: Uncategorized Tags: ,

Code Test

July 28, 2011 Leave a comment
def a() :
   a = b
   pass
#include<iostream>
using namespace std;

int main(){
  return 0;
}
Categories: Uncategorized Tags: ,
Follow

Get every new post delivered to your Inbox.