EE373A - MATLAB Information

Matlab

Note: This document was written for Matlab version 4, only minor changes were made to document access using Matlab version 5. Please notify akbar@stanford.edu if you notice any errors/omissions. (Thanx to James)

The name Matlab stands for Matrix Laboratory. It's an interactive mathematics program for performing scientific and engineering calculations. Moreover, it's an excellent tool for doing matrix manipulations commonly found in linear algebra. This manual outlines the steps to run Matlab on a MAC or on a UNIX workstation. It is also intended to familiarize you with the syntax and some basic Matlab commands so that before long, Matlab will become an essential part of your mathematical and signal processing experimentation!

Accessing Matlab And Writing M-files

Matlab is available on all the computers (workstations and MACS) in Sweet Hall, Terman 103, Terman Engineering Library and Tressider LAIR.

Workstations In Sweet Hall And Terman

You need a Leland account to run Matlab on the workstations. If you don't have one, telnet "elaine.Stanford.edu" or go to Sweet Hall, 2nd floor, then type "open" at the login prompt. Your account should be ready in 24 hours if your student ID is current. You will be able to remote login to any workstation to run Matlab and you can plot too if you have a graphics terminal. Read our newsgroup for details: su.class.ee264 on the Leland network.

If you are not familiar with UNIX, you can get help from the friendly and very helpful consultants in either Sweet Hall or Terman 103. The consultants in Sweet Hall are there during normal business hours, but Sweet Hall is open 24 hours a day. Helpful doc uments about UNIX are also available on the racks in Sweet Hall and Terman 103. Also, our TA has prepared a handout on basic UNIX commands.

The X windows system makes working on a Unix machine much easier. When you get your account:

  1. login and type x at the shell prompt to run the X Windows system.
  2. type matlab5 at the shell prompt in any xterm window. (You can run Matlab in the "console" window but you will not be able to display graphics.)
  3. Once in Matlab, the prompt ">>" is displayed. This means that you are now in the Matlab command window. Sometimes it is easier to store a sequence of commands, a program, in a script called an M-file. To make an M-file, yo u can choose whichever editor that you are most comfortable with. I prefer to use Emacs. (Pico, vi, and a simple X text editor are also available.)
  4. In the command window, type
    >>!emacs filename.m&
  5. Once you are done editing your program, save it with the Emacs command: ctrl-X, ctrl-S, or save the current buffer from the menu.
  6. To run the program, type the name of the file without the .m extension at the Matlab prompt in the command window.
    >>filename

Macintoshes In Tressider LAIR And Terman

  1. Open up the Matlab folder in the applications folder and double click on the icon named Matlab.
  2. If you are working on a MAC in the Terman library,
    (a) select chooser under the apple menu and click on the icon named appleshare. Make sure that Terman is highlighted in the appletalk zones window.
    (b) At the name prompt, type engineering and at the password prompt, type student.
    (c) A new window will appear. Select the icon courseware. You can copy Matlab from the applications folder in courseware onto the hard-drive.
  3. Clicking new under the File menu will bring up the editor environment for you to write your program in. Save your program as name.m and then run it with the command save and execute found under the File menu.

Note that in the Matlab programming environment, all commands to Matlab are typed after the prompt ">>". A command preceeded with the exclamation point, is a shell command to the operating system.

Quitting Matlab

  1. typing quit or exit will take you back to the operating system.
  2. At times, if you find it necessary to abort running a Matlab function without quitting, try: ctrl-C

Getting Help In Matlab


Matlab provides very effective on-line help.
  1. To get a list of all Matlab features, type:
    >> helpwin
  2. To get information on a specific command e.g. conv, type:
    >>help conv
    This will display a short description on how to use the command.
  3. To get a complete listing of the M-file of a specific feature/command e.g. conv , type:
    >>type conv
  4. For MAC users, there is an easier way to invoke the online help in Matlab.
    (a) Select About Matlab ... from the Apple menu.
    (b) A dialog box appears displaying a list of command groups. Double click on the group that contains the command you nee d help on.
    (c) Double click on any command and a help message will be displayed.

Data, Variables And Expressions In Matlab


As the name Matlab suggests, the one and only data object in Matlab is a matrix with real or complex elements. Scalars (single numbers) are interpreted as 1 x 1 matrices and character strings are treated as vectors of characters. Most Matlab statement s have the following form:
variable=expression

The expression is usually a function that returns outputs given some inputs. In Matlab, there is no need to declare the type or the dimension of the variable as required in high-level languages such as C, Pascal and the like. Being an interpreted lang uage, expressions typed in Matlab are immediately evaluated and the results displayed to the user. To suppress the results, you can place a semicolon at the end of a Matlab input statement.

Row And Column Vectors


A row vector is a matrix with only one row. In Matlab, row vectors can be entered as follows:
>> a=[1 2 3 4 5]
or
>> a=1:5;

Likewise, a column vector can be entered as follows:
>> b = [1
2
3
4
5 ]
or
>> b = [1; 2; 3; 4; 5]

Another method of writing the column vector b is to transpose the row vector a via the command:
>> b = a'

The Colon Operator

The colon, ":", is one of the most useful operators in Matlab. It is used
  1. to create vectors and matrices.
  2. to specify submatrices.
  3. in for loops.
Creating Matrices

We can use the colon to create vectors or matrices whose elements are regularly spaced. For example:
i:j is the same as [i,i+1,i+2,...,j] if j>i
i:n:jis the same as [i,i+n,i+2n,...,i+j] if n>0 and j>i or if n<0 and j<i

An example of creating a matrix using the colon operator is as follows:
>> x=[(1:2:5);(8:-1:6)]
will display
x = 1 3 5
8 7 6
Specifying Submatrices

We can also use the colon to select specific rows and columns of elements from vectors and matrices.
x(:,j) gives us the entire j-th column of the matrix x whereas
x(i,:) gives us the entire i-th row of the matrix x.
For example:
>> x(:,1:2)
ans = 1 3
8 7
Loops

The colon is used to designate the range of indices for a for loop. For example
for i=1:n
some operation depending on i
end

Formatting Numbers


There are different formats for the display of numbers. If approximately 15 significant digits are desired, then you could type format long at the Matlab prompt. To get information on other available output formats, type help format.

Saving and Clearing Variables

  1. The command "save" allows you to save workplace variables.
    >>save filename
    saves all workspace variables to a binary file named filename.mat.
    >>save filename X, Y
    saves only the variables X and Y.
    You can retrieve all these variables for future use and reference by typing:
    >> load filename.mat
  2. The command clear allows you to clear various quantities from the workspace. Type help save or help clear to get more information on these commands.

Plotting Graphs


Matlab has very powerful built-in functions for the following types of plots: linear x-y, loglog, semilog, polar, mesh, contour, bar charts and histograms. Graphs are displayed on a separate window in the Graphics window.

1-D Plotting


The simplest use of plot is
>> plot(x)
If x is a real vector, the sequence of elements in x will be plotted against the integers 1:length(x).
>> plot(x,y)

This command plots the sequence of elements in y against those in x. Note that for this command to be valid, the vectors x and y have to match in dimensions.

Labelling


To label the x-axis, type
>> xlabel('name')
To label the y-axis, type
>> ylabel('name')
To give a title to the plot, type
>> title('name of title')

Multiple Plots Per Page


You can have 1, 2 or 4 plots displayed per page using the subplot command. The subplot command takes one three-digit argument of the form ijk where the graphics window is partitioned into an i by jmatrix of small graphics windows. The digit kdenotes the numbering of the plot. For example, to have 4 plots in a page and begin plotting in the 3rd subplot, the command would be:
>> subplot(223);

Multiple Plots Per Graph


You can also have multiple plots per graph in two ways
  1. include more x - y vector pairs, e.g plot(x, yl, x, y2, x, y3)
  2. use the command hold. What this does is to hold the current plot so that you can superimpose subsequent plots onto it. Typing the command hold off will un-freeze the graphics window.

Printing

  1. MAC: To print your M-file and plots, you can select Print under the File menu. For the plots, make sure that the Graph window is the active window before you select Print.
  2. Workstations: To print your M-files, at the shell prompt, type
    lpr filename.m
    To print plots, make sure the Graph window is the active window, then type print at the Matiab prompt in the Matlab command window.