Why write this?

July 23, 2009 at 5:10 am (Uncategorized)

This might seem a rather single minded purpose for a blog. Luckily, single-minded blogs are all the rage these days.

How about I explain what brought me here.

In my undergraduate years I quickly became interested in neuroscience — how the brain works, how we can learn from nervous systems to build our own machines, and so on. Broadly speaking this field draws together people from electrical engineering, biology, and mathematical psychology. Since MATLAB is a popular system used in each of those fields, it is fairly entrenched in neuroscience. In any case, it was about 1999 and my first course on computer vision introduced me to MATLAB for image processing and linear algebra. Having some programming experience, I took to it well. My school had an unlimited site license for MATLAB, which I’m sure helped.

Between then and graduation, I took a number of other options in computer science and engineering. Programming-wise, this covered a wide range from assembly language to object orientation to functional programming. Then I worked as a software engineer in embedded medical devices before returning to school, and more importantly, to the brain. So in addition to a decade of experience with MATLAB, I know some other things.

Here back in neuroscience, everything is done in MATLAB, and now that I know some other things, it’s kind of stifling and awful.

What is MATLAB good for?

Let’s give you the good news. Say you are stuck with using MATLAB for a project. What do you get?

  • Arrays and matrices are fundamental to the language, given primary syntactic support — this is huge for anyone working with lots of data.
  • It gives you an interpreted command environment which is easy to get started in — you can work at the command line and see the immediate result of commands.
  • It gives you an easy interface to libraries for fast linear algebra
  • It gives you an integrated development environment with a debugger.
  • It’s pretty good at producing graphs, and gives you a lot of formatting options.
  • It gives you some very expressive ways to slice and cut up your arrays.
  • It’s garbage collected, so you don’t have to worry much about memory management as you schlep your huge arrays of data around.
  • In addition to a number of toolboxes with neat prepackaged algorithms, there’s a large user community all doing rather technical stuff with it.
  • You can build graphical user interfaces, putting a nice face on your code.
  • If you find something that can’t be done effectively with MATlBB, you can interface with extension files written in C, or with a direct interface to JAVA.

So it’s pretty good, right?

With the above list, it seems like a very good system. So why devote a blog to slagging it? After all there is imperfection and room for improvement in any system. Let’s not let unattainable ‘perfect’ of the future be the enemy of the ‘good’ today, right?

Well, MATLAB was obviously an improvement over FORTRAN for working scientists and engineers, in much the same way that Perl was an improvement over Sed, Awk and shell. So MATLAB was pretty good, in the ’90s, I suppose. Today, it’s an ancient language, in much the same way as Perl.

I have hundreds of thousands of lines of accumulated MATLAB code, so I’m pretty familiar with the language and environment. This provides a lot of good blog fodder, as I can just grep my source for ‘MATLAB stupidity’ in my code and I’ve material for a couple hundred posts on this blog. And at this point I’m pretty irked. The thing is, in that entire list I just listed, there isn’t a single thing that MATLAB does that it does better than other systems that are available right now for free. That is to say, going with MATLAB today is letting the ‘good last decade’ be the enemy of the ‘obviously better right now, today, attained.‘ It’s my aim to show you this.

So what should I use instead?

I don’t believe I wouldn’t be writing this if the alternatives weren’t available immediately at no cost! I will be focusing on two alternatives that you can start playing with today.

The first is the Python language, in conjunction with the Scipy group of libraries. Python is one of the most popular languages going, for good reason — it’s one of the easiest to write in, provides an interactive interpreter and a high degree of internal consistency. Scipy extends Python by bringing in high performance linear algebra libraries, which integrate nicely with Pythons existing semantics (full support for array slicing, for instance.) Throw in an open development process, a user community for the general Python language that is one of the world’s most active, and a user community just for Scipy that is at least as active as MATLAB’s, and you have something quite formidable.

The second system is R. I have only been working with it for a few days, and I can already tell that for statistical analysis of data and preparation of figures it simply blows the doors off MATLAB. I almost wept when I saw my first glimpse of the factors system, which slices up your data in ways that are extremely aggravating to do in MATLAB. Naturally is is a free, openly developed project. R is already the lingua franca of much statistics research in the same way that MATLAB is, unfortunately, the default language in my field — so if you are worried about leaving a technically knowledgeable user community, by switching to R you are most likely entering a user community that is even better for what you’re doing with the language. As for preparing publication quality figures? Take a look at the R graph gallery.

Okay, free, open-source projects, R and Python. What about Octave?

The aim of Octave is to produce a system that is largely compatible with the semantics of the MATLAB language. More realistically, Octave give you the MATLAB language ca. several years ago, with much poorer graphics output. Since much of the problem with MATLAB is that its language just sucks in terms of semantics, Octave is basically a non-starter. Many of the complaints I will make here apply just as well to Octave, as a matter of fact.

What about my existing MATLAB skills?

Honestly, it is really not that hard to pick up another language. It is much easier than it seems when you think of yourself as a busy person. But a car spinning its wheels in the mud is a pretty busy car, despite that it’s not getting nowhere fast.

Fear of having to learn new things! It’s understandable. We make our livings by doing what we do. The prospect of having to give up on some accumulated knowledge would seem to put you back on the same level as people starting out from square one. Except it doesn’t — there is lots of knowledge transfer especially with programming languages.

If you rely on a big pile of MATLAB code that is already written, condolences. You’re going to have to work with it. The funny thing is that by learning a new language you will learn new approaches to problems, and ways to organize your code that will improve your life in MATLAB. You might come to realize that MATLAB sucks, but with the knowledge of how it sucks, you will be able to better see where the walls are caving in with your system and shore them up. (An example is my ‘require’ function, which serves the same purpose as the with statement in Python. I will give it to you in a future post. It’s such a boon for error handling and resource management.)

That doesn’t always work — for instance, there’s no way to do continuations, or the limited continuations called “generators” in Python, so that (to draw from my field) experiments with interleaved staircases are a lot harder to organize. (Go check out generators — they’re very neat.)

But the details are for the rest of the blog.

About these ads

10 Comments

  1. Arunabh Das said,

    I agree that Matlab sucks. In a tenth of the time it takes to come up with a decent GUI in Matlab, I can come up with a rich UI in C# using WPF and have it crunch the numbers even more efficiently than Matlab. – Arunabh Das

  2. Yossarian said,

    totally agree – Matlab wants to be a tool that does everything, but it does nothing well.

  3. John said,

    I agree.

    The fact that one often needs to spend time to vectorize code (in my line of work anyhow) to yield reasonable run times and the clunky langage made me quit Matlab a long time ago. The fact that other langages enable to generate significantly shorter and more readable code helped me make the move also.

  4. Icarium said,

    Matlab has continuations? How do they work?

    • crowding said,

      Matlab doesn’t, but R and Python do. (Python’s are called “generators” and “coroutines” but they do give you about 90% of what true continuations do.)

      Here is a good example of the kind of things continuations can do. If you’ve written experiment control programs, such as with Psychtoolbox, you can probably see how these techniques would be helpful.

  5. Icarium said,

    Hm..I vaguely know python generators but I have never used the Psychtoolbox, can you give an example of the workflow it is used for or problems that it solves?

  6. ea said,

    Lately I’ve been programming tons with matlab. However, I started to find it annoying in several ways; in particular the lack of flexible data structures, some weird stuff regarding plots, and the how unbelievably hard is to make matlab to print an integer (there is a very nice post in this blog about the last). I will start to migrate a lot of things to python and to use matlab only to play around….

  7. kubaraczkowski said,

    Could you write something about the flashy, new Object-Oriented programming in Matlab?
    I wanted to implement some simple things that I learned in python and I went MAD… It’s like taking worst pieces of java, worst pieces of Matlab and gluing them together!
    Seriously, I thought I will go crazy when I tried to overload subscripting methods to implement some simple things…

    The one thing I like about Matlab (and OOP in it) is that you don’t need to reload anything. Even an method in a long-existing object can be tuned while running, which is cool. Would like to have it in python… (yes yes, reload() works, but I need to recreate everything).

    • crowding said,

      As far as I’ve gotten with matlab’s “new OO” is to verify that it’s spectacularly slow in the same way that their nested functions are spectacularly slow. The first thing Matlab does on invoking a method or nested function is it recursively enumerates everything that can possibly be reached from your method, twiddling reference counts.

      So, good luck building anything like useful data structures in your Matlab OO, when O(N) algorithms get automatically transformed into O(N^2) or worse.

      O(N) inserts on linked lists!!! What the hell.

      That’s as far as I got, and then I also noticed that translating my existing object-styled code into the new syntax would actually make the code longer.

      So, you see, I haven’t delved any further into the Matlab 7 OO stuff. Are the dispatch rules screwed up the same way Matlab 5 OO (yes, they tried this before, and even Mathworks buried the result in disgust) was screwed up? You try implementing your method plus(a,b) [or any multi-argument method] on a class, and the first thing you have to do is check whether it was A or B that is actually an instance of your class?

      Oh, god, it is still that bad!

      Goddamnit. Either single-dispatch on the first argument, or multiple dispatch. What MATLAB implements is “single mismatch.” It’s strictly less powerful than a single dispatch, The first thing you have to to on writing any method is to put a bunch of guards against being called from the wrong type of arguments.

      Christ, the VERY FIRST RULE of OO is, if you have to write type checks (isa/instanceof) more than one on a blue moon, you’re doing it wrong. Anyone can tell you this.

      • kubaraczkowski said,

        Heh, this is precisely why I thought it would be a nice topic for an entry :)
        I tried it once and got into an endless loop of saying: WTF!? How could it be SO wrong!? so I stopped trying…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 38 other followers

%d bloggers like this: