SD 372: Matlab Tutorial Tai Jing Moyung January 19, 2000

SD372: Matlab Tutorial

Winter 2000

Starting Matlab in Polaris

In Windows, navigate through the menu on the taskbar:

  1. Start
  2. Programs
  3. Scientific
  4. Matlab 5.2

Getting Help

Commands to know: The lookfor command helps to find MATLAB commands/functions that deals with a specific subject, but sometimes it may take a long time to search through everything.


  1. At the command prompt, type

    >> help help

    The second help can be substituted by any other help topic.

  2. Explore the help window. Open it using the menu bar on the MATLAB command window, or by typing at the command prompt

    >> helpwin

  3. Use any of the above to find the name of the MATLAB function to compute the eigenvectors and eigenvalues of a matrix.

  4. Find out how to use this function.

Working With Variables

Commands to know: In MATLAB, you do not need to declare variables before using them.


  1. Type the following at the command prompt and observe what happens:

    >> a = 5
    >> b = 10.1123
    >> c = [1;2]
    >> who
    >> clear a
    >> whos

  2. Use the save command to save the values of the variables b and c onto a file called bc.mat on your user directory and clear the workspace. You can use the command cd to change your current directory.

    >> save bc.mat b c
    >> clear
    >> who

  3. Reload the variables from the file:

    >> load bc

  4. Verify that both the variables b and c are loaded on your workspace.

Matrix, Matrix Operations, and Plotting


  1. The first part of the demo file on the course web page has basic examples on how to work with matrices. Try typing the uncommented lines at the command prompt to see how they work. (In the file, comments are preceded by %.)

  2. The following are useful as well, find out what they do:

  3. The second part of the same demo file illustrates how to make simple plots. Try generating the plots and note their differences. (Skip the part on user-defined functions for now.)

Using Script Files

Commands to know:

You can create script files with extension .m to run a series of commands all at once.


  1. Create a file on your user directory called plotxy.m. Using the MATLAB editor or any other text editor, type in the following lines and save the file:

    x = [-10:10];
    y = x.*x;
    axis([-10 10 -10 100]);
    grid on;
    title('The Quadratic Function');

  2. To run the script, the file has to be in the current directory or on MATLAB's search path. Check by typing at the command prompt

    >> pwd
    >> path

  3. Add your user directory to the search path by typing

    >> addpath N:\substitute_your_own_path

  4. Now you can run the script by typing

    >> plotxy

  5. You can remove the directory from the path by typing

    >> rmpath N:\substitute_your_own_path

User-defined Functions

You need to create a separate .m file for each user-defined function. The name of the .m file should be the same as that of the function.


  1. Copy the example function on the course web page into your local user directory.

  2. Now you can continue with the last section on the demo file dealing with user-defined functions.

Other Tidbits

  1. Check out the diary command. You can save the command history and results of the workspace onto a file.
  2. Recall commands by using the UP-ARROW and DOWN-ARROW keys at the command prompt.
  3. Recall specific commands by typing the first letter(s) and the ARROW keys. Ex.,

    >> plot

    Then hit the UP-ARROW key.