Wednesday, February 10, 2010

MATLAB

Where I work there are many different hats people wear and tools they use. Most people wear multiple hats. My main hat right now is that of software engineer. One of many other hats is that of algorithms/research engineer/scientist/something. A person wearing that hat is normally a level or two away from the implementation, but much more knowledgeable about the underlying science (than myself). It appears that the tool of choice for this abstraction level is MATLAB.

Since my job as a software engineer is to take the algorithms produced from the "MATLAB guys" and turn them into something that will run on a microprocessor, I must be aware of MATLAB to some degree. I consider myself fairly skillful with computer languages, but learning MATLAB has been similar to learning Mozart. Sure there are familiar things to latch onto, but the underlying way of thinking is very different.

I never took a class which used MATLAB, but I want to pick it up with a minimal amount of painfulness. There are times in which I have to dive deeply into MATLAB hackers' code, and the more I can do on my own the better. I have a few MATLAB books in my office, but I find that online references seem to serve me better. For that reason I am adding a MATLAB section to my programming languages page.

Here are some useful links I have found when dealing with MATLAB

  • http://www.mathworks.com/access/helpdesk/help/techdoc/ - MATLAB's documentation
  • http://web.mit.edu/18.06/www/MATLAB/matlab-cheatsheet.pdf - A little cheat sheet from an MIT course

    Now for a few random notes on things I have picked up while working in the MATLAB world.

  • help - this command will print out the first comment in the script associated with the MATLAB command. Make that comment a good one! Ex: help calcForce
  • keyboard - putting this in a script causes it to pause and give you a prompt. Useful if you want to see the world as your script sees it during debugging, or if you want to mess around with things.
  • MATLAB is all about the brackets: () {} []. Everything is an array. One of my coworkers tried to tell me this when I first started down the MATLAB path, but I did not take him so literally. Everything is an array. The brackets index into the arrays. A better explanation than I can provide may be found here.
  • As a unix guy, I like to roll my MATLAB scripts with -nodisplay whenever possible. Another neat trick is to use -r to feed MATLAB a command from the command line.
  • Like most systems, the path is important. You can adjust it by using addpath.
  • One would expect the normal behavior from save and load. However, there are some subtleties when working with them. In particular, where the data is stored and the layers of indirection depend on the parameters. One can load data into the workspace or into a variable.
  • global - marking variables as global in a script or before a script will make their values exposed after the script is run. This is helpful for debugging, as well as having multiple data products.
  • For writing to a file, check out fopen, fclose, and fprintf. If you are a C programmer then they should look familiar. There is also disp, so you should help that as well.

    Anything I left out? Any useful MATLAB learning tools you have to share? Please add them in the comments. I, for one, welcome our new MATLAB overlords.