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.