CS195-5: Introduction to Matlab

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 pyadolla (AT) cs .

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 cs195-5 directory (assuming you have created ~/courses/cs195-5) and run Matlab:

$ cd ~/courses/cs195-5
$ cp -r /course/cs195-5/matlab/tutorial/ .
$ cd tutorial
$ matlab &

Note: this will start Matlab (the current version, which is right now 7.2) in a separate window, containing a GUI. If you dislike the unfortunately rather slow and occassionally 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. You can also rely on addpath and genpath commands in Matlab to set the search path appropriately.

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 by keywords
>> 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. This tutorial will only describe the latter, however.

Instead of spending hours on reading long tutorials, we recommend you look at and run the tutorial code that we provide for you (it is also available at /course/cs195-5/matlab/tutorial/tutorial.m). This is well commented source code that should be easy to understand. 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.