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, ssh to elaine.Stanford.edu, or walk 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.
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
documents about UNIX are also available on the racks in Sweet Hall and
The X windows system makes working on a Unix machine much easier. When you
get your account:
- login and type
x at the shell prompt to run the X Windows
matlab at the shell prompt in any xterm window.
(You can run Matlab in the "console" window but you will not be able to
- Once in Matlab, the prompt "
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,
you can choose whichever editor that you are most comfortable with (such
as emacs, vi, pico), or use the editor built into Matlab version 6.
- For example, to use emacs, in the command window, type
- 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.
- To run the program, type the name of the file without the .m extension
at the Matlab prompt in the command window.
Macintoshes In Tressider LAIR And Terman
- Open up the Matlab folder in the applications folder and double click
on the icon named Matlab.
- If you are working on a Mac in the Terman library,
chooser under the apple menu and click on the icon
named appleshare. Make sure that Terman is highlighted in the appletalk
(b) At the name prompt, type
engineering and at the password
(c) A new window will appear. Select the icon courseware. You can copy Matlab
from the applications folder in courseware onto the hard-drive.
- 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.
exit will take you back to
the operating system.
- At times, if you find it necessary to abort running a Matlab function
without quitting, try:
Getting Help In Matlab
Matlab provides very effective on-line help.
- To get a list of all Matlab features, type:
- To get information on a specific command e.g. conv, type:
This will display a short description on how to use the command.
- To get a complete listing of the M-file of a specific feature/command
e.g. conv , type:
- For MAC users, there is an easier way to invoke the online help in
(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 need 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 statements have the following form:
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 language, 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
Likewise, a column vector can be entered as follows:
>> b = [1or
>> 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
- to create vectors and matrices.
- to specify submatrices.
- in for loops.
We can use the colon to create vectors or matrices whose elements are regularly
spaced. For example:
i:j is the same as
i:n:j is the same as
j>i or if
j<i An example of creating a matrix using the colon operator is as
x = 1 3 5
8 7 6
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
x(i,:) gives us the entire i-th row of the matrix x.
ans = 1 3
The colon is used to designate the range of indices for a for loop. For
some operation depending on i
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
Saving and Clearing Variables
- The command "
save" allows you to save workplace
saves all workspace variables to a binary file named
save filename X, Y
saves only the variables X and Y.
You can retrieve all these variables for future use and reference by
- The command
clear allows you to clear various quantities
from the workspace. Type
help save or
to get more information on these commands.
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
The simplest use of plot is
x is a real vector, the sequence of elements in
will be plotted against the integers
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
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 takes one three-digit argument
of the form
ijk where the graphics window is partitioned into
j matrix of small graphics windows. The
k denotes the numbering of the plot. For example, to
have 4 plots in a page and begin plotting in the 3rd subplot, the command
Multiple Plots Per Graph
You can also have multiple plots per graph in two ways
- include more x - y vector pairs, e.g
plot(x, yl, x, y2, x,
- 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.
- 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.
- Workstations: To print your M-files, at the shell prompt, type
To print plots, make sure the Graph window is the active window, then type
print at the Matiab prompt in the Matlab command window.