EE373A - MATLAB Information
Note: This document was written for Matlab version 4, only minor
changes were made to document access using Matlab version 5. Please
notify email@example.com 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
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:
- login and type
at the shell prompt to run the X Windows system.
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.)
- 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.)
- 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,
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
and at the password prompt, type
(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.
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 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:
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
Likewise, a column vector can be entered as follows:
>> b = [1or
>> b = [1; 2; 3; 4; 5]Another method of writing the column vector
is to transpose the row vector
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:
is the same as
i:n:jis the same as
An example of creating a matrix using the colon operator is as follows:
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.
gives us the entire j-th column of the matrix x whereas
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 example
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 variables.
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 typing:
- The command
allows you to clear various quantities from the workspace. Type
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 window.
The simplest use of plot is
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 dimensions.
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
command takes one three-digit argument of the form
where the graphics window is partitioned into an
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:
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, y3)
- 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
at the Matiab prompt in the Matlab command window.