If this is your first session with MATLAB, go through the following MATLAB session and then do another session with a few of the functions in section II.3.

For technical details on directories etc., see the heading TECHNICAL NOTES in the introduction.

  1. To start MATLAB.
  2. Click the icon (windows) or type matlab (UNIX etc.).

  3. To quit MATLAB.
  4. Type quit.

  5. To save your work in a diary file calcul1.dia.

    Open a "diary" using the diary command: diary calcul1a.dia. (the extension .dia helps you identify your diary files)

    To toggle the diary: diary off, diary on.

    To end the diary session: diary off.


    In window environments you can, in principle, do everything without a diary. In practice however, especially when you are learning or when you run a program more than once, or if you run several programs, you are going to spend a lot of frustrating time cutting and dragging. Diaries permit you to direct the output of different trials or programs to different files. Those files that you want to retain will be small and easily edited.

    Suppose that you have a functions tele1.m and tele2.m that you wrote to analyse a telephone system. You want to run them, or debug them, for different sets of values. An efficient way to keep track of what you doing is to create different diaries for different trials and lable them: tele1a.dia, tele1b.dia ..., tele2a.dia, tele2b.dia ... . This way you when you look at your files, you will immediately see that you are dealing with the first, second ... diaries for functions tele1.m and tele2.m. You can check these diaries in the middle of a session and easily edit and print them.

  7. Editing in the middle of a session.

    Use up arrow, down arrow, delete/backspace, or ENTER commands on the keyboard.

    [Note: These commands are very useful as they help avoid the necessity of retyping an entire line. These commands are also valid in window versions

  8. Returning to the system.

    If you are using MATLAB on UNIX, LINUX or DOS , etc. use ! followed by commands such as ls, or by giving the name of a text editor. At the end of the system operation control returns to MATLAB.


    Use % to place comments at the MATLAB prompt or after MATLAB commands.

  10. List of variables.

    Type whos.

  11. To clear the work space.

    Type clear.
    To clear just some variables: clear A1, ans

  12. To clear the screen (not the work space).

    Type clc (= clear command).


a. MATLAB is case sensitive; thus variables A and a are not the same. [If you really do not like case sensitive programs then use casesen off and casesen on to toggle, but first read the next suggestion].

b. To help you distinguish matrices from vectors and scalars it is suggested that you use uppercase for the former and lowercase for the latter, e.g. A or STORE for matrices and a or vect_1 for scalars and vectors.

c. Variable names can be any length and may contain an underbar e.g. MATRIX_1. Do not write MATRIX-1, because MATLAB will think that you are subtracting. Do not write 1_MATRIX , i.e. don't start the name with a number) as MATLAB thinks that you are dealing with a number.

d. Do not use the name of a MATLAB function for your variables because MATLAB will think that you are trying to evaluate the function and forgot the ( ). Thus since det( ) is a built in MATLAB function, if you want to designate the determinant of your output matrix, call it deter or d, but not det! A very common error is to write something similar to:

>> sum = 2 + 5

If you next use the MATLAB function sum( ) you will find that it does not work!

>> b = [2 5]
>> sum(b)

MATLAB responds with the error message:

" ???? index exceeds matrix dimensions. "

Instead of calling your variable sum, call it sum1 or total. Similarly do not use prod or mean as variable names.

e. MATLAB functions are lowercase (some versions accept uppercase) and use parentheses. The name usually gives a good idea of what the function does e.g inv(A) finds the inverse of A and rref(A) give the reduced row echelon form of A. Some functions however return a pair of matrices (see Section I.3).

f. MATLAB is often able to interpret statements that are incorrect in the sense that they do not stand for what you wanted them to stand for. In particular you can often get away without using parentheses, but not only may the answer be different from what you had intended (see II.1), but also later you won't be able to understand what you had written. So USE PARENTHESES. Always test programs and defined functions on examples for which you can do a hand check.

g. MATLAB is now fairly stable as far as the names of commands are concerned. If you use a command that has been replaced, you will usually see a message that gives you the new form of the command.

h. If you quit MATLAB, then return to MATLAB and use a diary name that you have previously used, then MATLAB adds to the previous diary file.



Here is a sample MATLAB session.

First we call up MATLAB either by clicking on the icon (window systems) or by typing "matlab" at the system command (UNIX etc.):

{superior:1} matlab

Now open a "diary" file called calcul1.dia in which to save the commands and output of the session. If you add the suffix ".dia" it will be easier to locate the diary when you go to edit it.

>> diary calcul1.dia

To enter a matrix to which we assign the variable name A, type in the matrix separating the rows by a semicolon. Push ENTER and MATLAB automatically types out the variable name and the answer, unless commanded not to do so by the use of a semicolon after:

>> A = [.2 .6; .4 .3]

A =

0.2000 0.6000
0.4000 0.3000

I now want to find the inverse matrix which I call A_INV.

% inv( ) is the MATLAB function for finding inverses:

>> A_INV = inv(A)


-1.6667 3.3333
2.2222 -1.1111

Check using multiplication, there is no need to give a variable name. Whenever no variable name has been assigned to an operation MATLAB assigns the value to the variable ans.:

>> A*A_inv

ans =

1.0000 0.0000
-0.0000 1.0000

I now decide to see what happens if I replace the .4 by .1. Instead of retyping the entire matrix, I use the up arrow until I arrive at the data statement. By using the arrows, DEL or BACKSPACE, I change the .4 to .1. I also decide to change the name of the matrix to A2.When ENTER is pushed the new A2 appears:

>> A2 = [.2 .6; .1 .3]

A2 =

0.2000 0.6000
0.1000 0.3000

If I now use the up arrow to go to A_inv and change A to A2 and then push RETURN, the command is repeated. Or I could just retype

>> A2_INV = inv(A2)

Warning: Matrix is singular to working precision.

MATLAB has come up with an error message.

I could make more changes, but I have done enough on this problem, so I close the diary

>> diary off

All of a sudden, I remember that I want to find the determinant of A. So I turn the diary on and make sure that I turn it off again at the end. The new material is added at the end of the diary.

>> diary on

In order to continue with the computations, the names of the variables and their dimensions must be known. This is done via the MATLAB command whos:

>> whos

2 by 2
2 by 2
2 by 2
2 by 2
2 by 2

The variable ans came about from the check on A_INV to which no variable name had been assigned. If we had done this several times only the last ans is kept.

I now proceed

>> determinant = det(A)

determinant =


>> diary off     % close the diary

The additional material has been added to the diary

We could now start a whole new set of computations; first clear all the variables and then open a new diary:

>> clear

>> diary calcul2.dia

or we could just clear A2 and its inverse

>> clear A2, A2_INV

We now go through a series of calculations using some of the operations of section II.1 or the functions of section I.3.

When we are done we close the diary:

>> diary off

At this point I want to see what the results were. There is only one problem, I can't remember the name of the first diary!! (We could also have used the UP ARROW to find the original diary command.)

For window systems click on the file listings restricting your search to *.dia files. For UNIX systems type

>> ! ls *.dia

From the file listing, I remember that the diary name was calcul1.dia.

I can now continue my calculations, with the same or another diary. If I want a clear screen I use clc

>> clc

To leave MATLAB:

>> quit

The file calcul1.dia can now be edited and printed using a text editor.


Back to the Table of Contents

Web Site Training provided by Kathryn Charis Apunen Washburn summer 2001