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!

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
`x`

at the shell prompt to run the X Windows system. - 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.) - 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

`>>!emacs filename.m&`

- 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.

`>>filename`

- 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,

(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. - 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.

- typing
`quit`

or`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:
`ctrl-C`

Matlab provides very effective on-line help.

- To get a list of all Matlab features, type:

>> helpwin - 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. - To get a complete listing of the M-file of a specific feature/command e.g. conv , type:

`>>type conv`

- 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.

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.

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'`

- 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,i+1,i+2,...,j]`

if
`j>i`

`i:n:j`

is 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

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

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

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`

.- 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`

- 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.

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

` >> 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.

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')`

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
`j`

matrix of small graphics windows. The digit
`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 would be:`>> subplot(223);`

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

`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.