CS195f: Introduction to Matlab

Foreword

This purpose of this tutorial is to give you a brief introduction to Matlab (http://www.mathworks.com/) to make it easier for you to implement the assignments for CS 195f. You might also want to use Matlab for your home assignments. There are several reasons why we think it is worthwile getting familiar with it: First of all, Matlab will most likely save you quite a bit of time, although it may not seem like it initially. Also, Matlab is a standard software tool that's used in research and in industry, so it's a good thing to know it. As a bonus, several other CS courses, e.g. CS 143 and CS 224, use it, too.

There's an abundance of information and Matlab tutorials on the Web. If you don't like any particular one, just go to Google and you'll surely find something useful. A few good links are:

However, a lot of these tutorials are quite lengthy and include more detail than you will probably need. Hence our summary here:

Getting Access to Matlab

Everyone with a CS account has access to various versions of Matlab on all of the Linux systems. By now, everyone should have a CS account, but if you don't have one, please email dqsun (AT) cs DOT brown DOT edu.

If you prefer to work on your own computer, you can get Matlab from CIS for the most important platforms:

However, all of these only work while you are on the campus network, because they need to access a license server. If you want to use these Matlab versions from outside the campus, you will most likely have to get onto the campus network using VPN. You're entirely on your own on this though.

Getting Started

Copy the sample file into your cs195f directory (assuming you have created ~/courses/cs195f) and run Matlab:

$ cd ~/courses/cs195f
$ cp -r /course/cs195f/matlab_tutorial/ .
$ cd matlab_tutorial
$ matlab &

Calling matlab will automatically run the latest verion of Matlab (currently 7.4). If you want to run a specific version of Matlab, you have to use the full executable name. For example, if you want to run Matlab version 6.5, you need to call matlab65.

If you dislike the unfortunately rather slow and buggy 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 load 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>. 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_name>
and for searching
>> lookfor <keyword>

Try typing in "help sum" and "lookfor logarithm". The lookfor command may take around 10 seconds to finish.

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 recommend you look at and run the tutorial code that we provide for you. This is well commented source code that should be easy to understand. 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, 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.