Matlab Introduction
CSC 295 - Computer Vision - Weinman
Background
This primer will give you a brief introduction to Matlab (http://mathworks.com).
This is the software that we will be using to complete our laboratory
assignments for this class. While it may seem difficult at first,
as the course progresses, you will be satisfied at how quickly it
allows you to do many complicated things with relatively little pain.
In addition, it is a standard package used widely in research and
industry, so becoming familiar with it may be of long term utility.
There are a plethora of tutorials for Matlab on the web, including
those written by the company that produces it. Using Google, you are
likely to find more than you would need. However, here are a few potentially
useful leads:
Many of these are lengthy with more details than you may need just
now. Thus, we will get started with a very brief primer and you'll
do a laboratory to reinforce some of the basics.
Getting Started
Change to the directory containing code for our course and run Matlab:
-
$ cd ~weinman/courses/CSC295/toolbox
$ matlab &
Calling matlab will automatically run the latest verion of Matlab.
If you dislike the GUI you can run the command line mode using:
-
$ matlab -nodesktop
The directory you are in when you run Matlab will later be important,
because Matlab will automatically loads any .m files (Matlab
programs) that are in your current directory. Nevertheless, if you
started from the wrong directory, you can either change the directory
using the GUI or you can use
-
>> cd directory
at the Matlab command prompt. Other Unix-like shell command such as
ls and pwd are supported as well.
To quit Matlab, type:
-
>> quit
Matlab has two different kinds of "programs":
- Scripts
- Scripts are simply a number of Matlab commands stored
in a file with the extension ".m".
- Functions
- Functions are what they are in other programming languages.
The important thing about functions is that each function has to be
in a separate file with the same name as the function (plus the suffix
".m"). For example, you would save the function
my_test in a file called my_test.m. Function files also have a special
syntax, but more about that later. Getting Help
The Matlab GUI comes with a pretty extensive help system. If you don't
like GUIs or you just need to quickly look something up, you can use
the following commands:
-
>> help function
and for searching
-
>> lookfor keyword
Matlab Commands
The name Matlab comes from MATrix LABoratory, and hence it is not
surprising that matrix operations are the most important types of
computation in Matlab. Actually, Matlab supports arbitrary n-dimensional
arrays, of which matrices and vectors are just special cases. We will
only describe the latter, however. Images are nothing but matrices,
so it should be fairly obvious why matrix algebra will be important
for us.
Instead of spending hours on reading long tutorials, we have provided
some starter tutorial code for you: ../misc/MatlabTutorialCode.html
This is well commented source code that should be easy to understand.
Study it before the lab.
The most important Matlab commands are described in the script intro.m.
You can run it all at once by changing Matlab's work directory and
typing intro, but this way you will not be able to see and
learn very much. Instead, you should run it piece by piece: If you
use the Matlab GUI, you should load the file,
-
>> edit intro.m
then bit by bit mark a few lines of code at a time and run the marked
code by choosing "Evaluate Selection" from the context
menu (right mouse click). The output of the operations appears in
the main window. If you don't like the GUI, you can copy and paste
a few lines of code at a time into the command line and after pressing
enter the last pasted code block is evaluated.
If you've looked though that file and read the help pages for a few
important functions you should know enough about Matlab for our purposes.
If you're still curious for more, have a look at the Matlab Primer,
which contains more detail and explains additional commands.
We'll reinforce this in class with a some lab exercises.
Acknowledgments
Adapted from "CS143: Introduction to Matlab," Stefan Roth
and Teodor Moldovan.