Showing posts with label MATLAB. Show all posts
Showing posts with label MATLAB. Show all posts

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.

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.