Getting Started on MATLAB

Matlab

This page explains your first steps in MATLAB. You could use any editor you wish, but let's suppose you use the text editor built into MATLAB. If you use Microsoft Word, make sure to save your files not in Word format, but in Text-only format.

Availability

MATLAB is available on Click on the platform of your choice to get more information. There are tiny differences even within the MATLAB syntax - logical AND is && in Coover and & in Carver. help relop should tell you what to do there.

First steps

  1. Suppose you have started MATLAB and the cursor is blinking inside the MATLAB command window. Things you type are written in boldface below.
  2. You can type things like 4+5, then hit the 'return' key.
  3. MATLAB responds with a line 'ans = 9'. You can modify the command until you hit return.
  4. Let's do some linear algebra. You can have vectors: type v=[3 4 5 6]
  5. ... and matrices: type A = [1 2 3 4; 2 3 4 5]
  6. You can multiply matrices: try A*v
  7. That did not work - MATLAB tells you why not.
  8. So we need a column vector! type v=[3;4;5;6] - note the brackets and the semicolon ; separating rows
  9. If you hit the UpArrow key, the last command you entered reappears. Hit UpArrow again and A*v should reappear. You could modify it, but for now just hit the return key again - now it works!
  10. Of course we want to solve systems. Type u=[1;2].
  11. To solve Ax=u, type x=A\u (the backslash is below the delete key).
  12. The MATLAB command help displays a list of help topics. Eg help matfun has many functions for manipulating matrices. Try it!
  13. MATLAB also has a HELP button (not in the Project Vincent version)
  14. Perhaps more than what you want to know is at MATLAB help page of Carver Lab.
  15. If all else fails, ask other people in the lab or just knock on my door.

Running MATLAB using M-Files

You can work with MATLAB interactively as above, but I recommend to get into the habit of working from a file right away. This makes it much easier to trace back your steps, correct errors and produce output that can be verified. M-files contain a sequence of commands just like those you type in the command window.
  1. Pull down the File menu and select New, then select M-File from the 'New'-menu. A new window 'Untitled.m' appears. You can type in MATLAB commands. You can also copy and paste them in that file, edit them etc.
  2. Download the matlab example files below, on Macs preferably into the 'User' folder.
  3. You can also start with one of these and edit/enlarge it.
  4. See the Mac page on how to use Mac editors.
  5. In the MATLAB editor window, press Command-E for executing your M-file (on Macs only).
  6. In the MATLAB command window, you execute an M-file (eg myfirst.m) by typing its name (eg myfirst).
  7. If you want to write a MATLAB function, include a headline comment describing the function, and then eg

    function y = square(x)

    after which can follow any MATLAB commands. At the end of the function, y should have some value (eg y=x^2), which is then returned. You can call your function by doing z = square(t); (similarly, any MATLAB function). Several input variables are possible, but only one output variable - make the output a vector if it contains several numbers. Typing help square displays the headline comment(s) you wrote.
  8. From time to time, save your file under some sensible name!
  9. When you are finished, copy your file to floppy disk or to your Project Vincent space. The hard disks of the machines in the lab are wiped clean from time to time.

Sample M-Files

You can download these, execute them and modify them. To quit Matlab, type quit.