MATLAB Quick-Start Guide


These pages are designed to be a useful tutorial to get started quickly on using MATLAB for data analysis. MATLAB is a sophisticated software tool for numerical analysis. There are versions for many different platforms; in particular, MATLAB is supported on all Athena platforms. I tried several Athena programs when I was taking Junior Lab, and I found that MATLAB was the one the best suited me for data analysis.

You might want to try the stuff on MATLAB as you read it below to convince yourself of how easy it can really be... And once you've mastered the basics, you can use MATLAB's help system to learn about other commands, built-in functions, and variations of the basic commands below.


This page is now stable, but not static. I will continue to add useful information as it is requested or as it occurs to me.

Last Update: Added section on curve fitting.


Contents

  1. Preliminaries

  2. Matrices

  3. Graphing

  4. Importing and Exporting Data

  5. Curve Fitting


1. Preliminaries

Starting Matlab on Athena

To start MATLAB, you will need to do the following from an xterm window:

athena% add matlab
athena% matlab &

The MATLAB command window will then appear. Your graphical output will appear in other ad hoc windows when you issue the appropriate commands.

Getting help

Once you're inside MATLAB, you can get help by using the help command. You can use the following commands:

>> help
will get you a listing of all the categories of functions for which help is available.

>> help category_name
will get you a list of the commands for that category.

>> help command_name
will get you the help information for that specific command.

>> doc
will load up the MATLAB on Athena webpage, using Mosaic as the default browser (to change browsers, define a MOSAIC environment variable with the name of your preferred browser).

>> demo
will take you through a tour of MATLAB's capabilities.

Echoing Commands

When MATLAB finishes executing a command, it typically echoes whatever value was calculated in that command. Sometimes this is useful in checking that MATLAB is doing what you intend it to do. At other times it is inconvenient. If you add a semicolon (;) at the end of your command, MATLAB will not print the value it has just calculated. Compare:
>> a=5
a =
     5
>> a=5;
>> a
a =
     5

For more info...


2. Matrices

MATLAB deals exclusively with matrices. Matrices can be of any size including empty (zero rows and/or columns), one-by-one (scalar), n-by-1 (column vector), 1-by-m (row vector), or n-by-m (proper rectangular). Matrices can be real or complex, and sparse or full, although in most laboratory applications you won't need to worry about this.

Creating vectors

Creating matrices

Manipulating matrices


3. Graphing

XY Plots

Titles and labels

Errorbars

errorbar(x,y,l,u,symbol) draws the points xy using the format in symbol, with errorbars that extend a distance of l below the corresponding point and u above the corresponding point. X, y, l and u are vectors of the same size.

3-D Plots

See MESH, SURF, WATERFALL.

4. Importing and Exporting Data

The following information is taken verbatim from the online help:

Saving files

 SAVE   Save workspace variables on disk.
        SAVE fname saves all workspace variables to the binary "MAT-file"
        named fname.mat. The data may be retrieved with LOAD. Omitting the
        filename causes SAVE to use the default filename "matlab.mat".
 
        SAVE fname X  saves only X.
        SAVE fname X Y Z  saves X, Y, and Z.
 
        SAVE fname X Y Z  -ascii  uses 8-digit ASCII form instead of binary.
        SAVE fname X Y Z  -ascii -double  uses 16-digit ASCII form.
        SAVE fname X Y Z  -ascii -double -tabs  delimits with tabs.
 
        If fname is "stdio", SAVE sends the data to standard output.
 

Loading Files

 LOAD   Retrieve variables from disk.
        LOAD fname  retrieves the variables from the MAT-file 'fname.mat'.
        LOAD, by itself, loads from the file named 'matlab.mat'.
        LOAD xxx.yyy  reads the ASCII file xxx.yyy, which must contain a
        rectangular array of numeric data, arranged in m lines with n values
        in each line.  The result is an m-by-n matrix named xxx.
 
        To load an ASCII file that does not have a filename extension, use
        LOAD fname -ascii. Otherwise MATLAB adds the extension '.mat' and
        tries to load it as a MAT-file. To load a MAT-file that does NOT
        have a '.mat' extension, use LOAD fname.ext -mat.
 
        If fname is "stdio", LOAD reads from standard input.
 

Loading raw data

Have your data in a text file organized in whatever way you want in matrix form, that is, tabs or blanks to separate columns, and newlines to separate rows. Then, once you're in MATLAB, do
load filename -ascii.
This will load your data into a matrix of the appropriate size whose name is filename.

Curve-Fitting

MATLAB has impressive and quick! facilities for curve fitting. If you really want to understand the theory behind curve fitting (and I suggest that you do, because it's really quite nifty) pick up a copy of Numerical Recipes. Look through the sections on Levenberg-Marquadt. You'll probably learn a lot. You can find this book, among other places, at the Hayden basement cluster. It's Athena rackware, so you can't take it, but pursuant with copyright restrictions you can copy brief passages for your own academic, non-profit use.

Read on about this most useful of features...

Polynomial fits

With your data in the the vectors, say, x and y, the command
>> [p, S]=polyfit(x,y,n);
will fit your data with a polynomial of degree n, and return the coefficients in p (in descending powers of x) and an error-related matrix in S.

Fitting to arbitrary functions

Again with your data pairs in x and y, the command
>> [p, Yfitted] = curvefitnl(x,y,fstring,guess);
will fit an arbitrary function to your data. fstring is a string containg the symbolic function to fit; for instance 'p(1)*sin( p(2).*x ).^2' would be used to fit a squared sine wave of amplitude p(1) and angular frequency p(2). The vector guess is just a row vector with your initial guessses for the parameters p(1)..p(n); for instance, [1 2*pi].

p returns the parameters p(1)..p(n) for which you fitted, and Yfitted returns the values of y predicted from the actual values of x according to the solved for parameters p.

NOTE: Chances are you'll have to succesively refine your initial guesses if MATLAB reports that that it is having trouble getting a clean fit.


This page written by:
Victor Chudnovsky (vchudnov@mit.edu).