MATLAB Usage Tutorial

 

The Matlab Environment:

  1. Create a directory in your home directory (on H:) to store your Matlab work there.
  2. Download test images for Assignment 1 from http://www.cs.sfu.ca/CC/414/li/material/work/PA1/PA1-testimages/ and save them in the directory you created.
  3. Start Matlab.
  4. In Matlab, type "cd h:\$home" where $home is your directory for the Matlab work. That will set the current working directory to your home directory, so that you can read and save files in your home directory.
  5. Matlab functions are represented as M-files which are text files containing Matlab code. A Matlab M-file has a .m extension. The M-file is run by typing its name (without the extension). You should save your functions in M-files as well.
  6. To edit an M-file or create a new file, either open a file from a menu or the toolbar, or type "edit $filename.m" where filename is the name of the file to edit. It is recommended to use the Matlab editor for writing functions since it is also a debugger. You can place breakpoints and step through your M-file code.
  7. To learn Matlab syntax, refer to the Matlab books in CSIL. However you can find help online by typing "help $function" where $function is the function you want to look up. If you are looking for a keyword in all Matlab functions type "lookfor $keyword". You can also use a help window by typing "helpwin" or a Matlab manual on the web by typing "helpdesk" (you can also run them from the menu).

 

Matlab Language Tips

  1. Matlab is a linear algebra oriented command line interpreter.
  2. You should try to define steps in the algorithm in terms of matrix manipulations if possible. It is a lot more efficient than iteration.
  3. Variables do no have to be declared, but all variables have a type. You can cast variables to other type if necessary.
  4. To access an element in matrix A use: "A(x,y)". A colon operator ":" is a wildcard, so "A(:,y)" would mean to return all of column y of matrix A. Similarly "A(1:5,y)" means generate an array of indices 1 through 5 and return column y rows 1-5. Square bracket operators are used for array concatenation, e.g.: "B = [A; 5 1 3]" will create matrix B with [5 1 3] as its last row. A ";" means end of the row.
  5. Matrices have different meaning for their operators than scalars. E.g. "*" means matrix multiply, not multiply two matrices element-wise. To have an operator apply element-wise precede the operator with a period, so that "A.*B" would mean multiply each element of matrix A by the corresponding element of matrix B.
  6. The image processing toolbox that has most of the computer vision algorithms implemented is called images/images in the helpwin.
  7. Type demo, or choose "Examples and Demos" from the help menu, and then click on Toolboxes and then Image Processing. You can run various demos to experiment with image processing techniques. Also experiment with "sample.m".