The (Sometimes Very Annoying) Differences Between IDL and Matlab...
Everyone has their opinions about coding languages and analysis software. I've sat through entire meetings listening to people bicker over whether Matlab is better than IDL, or vice versa. And then there's always a couple of people in the corner trying to say that both of them are a waste of money since Python can do everything they can do, but better.
The purpose of this web page is not to make any judgements about which analysis package is numero uno. Rather, it's to point out the very subtle differences between the two. If you are ever tasked with converting a script from one language to the other, they are differences that could have you so frustrated that you are screaming at the ceiling and banging at your desk with all your might.
Starting IDL vs. Starting Matlab
My personal preference for running either of these two applications is to do so in a command line setting where the machine isn't bogged down with memory intensive graphical user interface stuff. In IDL, this is pretty easy to remember since there are two separate commands for the command line version and GUI version:
Starting the IDL Development Environment
Starting the IDL Command Line
In Matlab, the default behavior is to start up the very memory hungry GUI. When I'm running this remotely, it is agonizingly slow, so I usually choose to run it from the command line instead. The problem is that the latter requires using some arguments that are difficult to remember, so I always have to look them up if it's been awhile since I last used it.
Starting the Matlab GUI
Starting the Matlab Command Line
$ matlab -nodesktop -nojvm -nosplash
The arguments mean the following:
- -nodesktop - prevents the desktop GUI
- -nojvm - prevents the java virtual machine from starting (you'll get an error message if you try using -nodesktop without -nojvm)
- -nosplash - prevents the start-up splash screen
Array Indices - Zero Vs. One
Let's start off with the obvious one. The one that you probably discovered within minutes of switching from one program to the other: indexing arrays.
If you're used to coding in C or C++ (or practically any other language, for that matter), then IDL will make a lot of sense to you since array indices start with 0 and go to N-1, where N is the size of your array:
IDL> print, a(0:9)
0 1 2 3 4 5 6 7 8 9
Here, we declare an array of long integers with N=10. And when we access the first element in the array, we must use the index of 0. We must also be careful to use N-1 as the final subscript, i.e. a(0:9).
In Matlab, this looks a little different:
0 1 2 3 4 5 6 7 8 9
We are again declaring an array with N=10, but to access the first element, we must use an index of 1.
For Loops and Counters
Let me tell you, folks, this one had me debugging code for almost an entire afternoon. In this case, I'm torn as to which language is doing things better. The Matlab version makes more intuitive sense to me, but IDL is doing things a bit more like the regular convention used in C/C++ and other languages I've used.
In most cases where a for loop is used, one doesn't pay explicit attention to the value of the counter after exiting the loop. But if you happen to need that value of the counter (for instance, after using a break statement), you'll find very different behavior in IDL and Matlab.
IDL> for i=0, 9 do print, i, format='(I,$)'
0 1 2 3 4 5 6 7 8 9
IDL> print, i 10
You can see that after exiting the for loop, the counter is incremented one more time so that it has a final value of 10. This is the same behavior as you'll find if you compile a C program and run it. However, in Matlab, you'll get a different behavior.
>> for i=0:9; fprintf('%d\t', i); end; fprintf('\n')
0 1 2 3 4 5 6 7 8 9
i = 9
Here the counter stays put at 9 instead of incrementing again!
The moral of the story is: if you are translating code between Matlab and IDL, beware of this this difference or you may be debugging for hours.
Setting Up Your Path
Setting up your path properly is a very critical operation when it comes to an application like Matlab or IDL. If you're not sure what setting a path does, it basically makes it so that you can call a script or function no matter what your current working directory is. Matlab and IDL will search all the directories in your path for the script/function you're trying to call and use the first instance it finds (which means you need to be careful to not have multiple copies of a script/function on your machine!).
In my opinion, IDL wins this one. That's because IDL allows you to use recursion in adding paths. So if you add a directory to your IDL_PATH and place a "+" before it, it will add all the subdirectories in that directory, and the subdirectories in those directories, and so on. Matlab makes you use the "genpath" function to produce a string containing all the subdirectories. Sounds all well and good, but that string might be super long and annoying to cut and paste.
Setting your IDL Path
On Mac/Unix, setting up your IDL path involves adding an environmental variable called "IDL_PATH". If you're not familiar with environmental variables, do some reading on them. For the case of tcsh or csh users, adding a line like this to your .cshrc file will do the job.
setenv IDL_PATH "+/Users/myusername/IDL_Programs/":"/Applications/itt/idl"
This example assumes that you've collected all your IDL scripts/functions in a directory called IDL_Programs in your home directory. The "+" means that all subdirectories found therein will be included as well. The next directory is a standrard IDL directory. I always like to put those second because if I've created a script that has the same name as a native IDL script, I like mine to be the one that gets called.
On Windows, I've always just used the IDE to set the path. See David Fanning's page on this for more details.
Setting your Matlab Path
While doing a recursive addition to your path is more difficult in Matlab, the process of adding a single directory is a bit easier, in my opinion. To do this, just click on the "Set Path" icon at the top of the Matlab IDE (under the Home tab). This brings up a dialog box that allows you to select directories, move them up or down in order, and save whatever you've added or removed.
This is really simple. The problem is, it's really hard to find instructions on this in the Matlab documentation. I spent a long time searching for this (I never saw the "Set Path" icon, stupid me) and trying to alter the pathdef.m file and the startup.m file instead. The latter does allow you to add directories to your path. The former can actually corrupt your Matlab session, so don't try it.