Monday, January 3, 2011

MATLAB tutorial

I have mentioned before that many of the folks at my office use MATLAB. Since my office has historically been an "algorithms" shop, most of the ideas are prototyped in MATLAB before a software engineer (such as myself) implements them in a traditional programming language. I have picked up some MATLAB, but still felt I needed a better grasp on the tool. So I plunged into the help and online tutorials. Here are some nuggets I have learned in addition to my original MATLAB post:
  • MathWorks provides a good collection of MATLAB tutorials on their website. This includes links to other people's guides and their own audiovisual walkthroughs.
  • I liked a linked MATLAB walkthrough from MIT. It is fairly concise and covers most of what you need to get going, except for cell arrays (which is what I really wanted to learn better). The different entries in the table of contents link to a single webpage.
  • The main confusion I have with MATLAB is with the array notations. As a C programmer I have a certain way of looking at arrays, but someone brought up in the MATLAB universe doesn't even use the same vocabulary to describe things. Hence the difficulty. Here are some notes:
    • : (colon) - indicates a range, typically a domain
    • [] (brackets) - define a matrix or vector
    • ; (semicolon) - new row for matrix defined in brackets
    • () (parentheses) - indexes to a vector or matrix
    • , (comma) - indexing into next dimension of a matrix
    • .() (dot) - access named fields of a structure
    • {} (braces) - similar to bracket and parentheses, only internal structure is preserved
    Typing doc paren or help paren will bring up MATLAB's description of these operators.
  • Speaking of doc and help, they both are similar to the linux command man. doc opens up the help browser, whereas help prints out information at the prompt.
  • I would suggest using help and doc on a number of useful commands: path (where MATLAB looks for things, plot (make the fun graphs for which MATLAB is known), clear (disassociate a variable), and nargin (number of arguments to a function).
  • The Programming Roadmap on the MathWorks site talks about advanced function features. Use help/doc on varagin, varargout, and funfun. MATLAB can even have anonymous functions using @, but I doubt the scientists and work want me using λ functions.
  • Need/want to play with MATLAB but do not want to fork over private funds for a license? Try Octave!
I now feel much more confident in my MATLAB skills, or at least my knowledge of MATLAB resources. Hopefully I can put them to good use when given the opportunity.