UB Information Technology

Documentation

MATLAB for Windows

Documentation print footer; appears at "bottom" of first page.

Adobe Reader 7 for Windows

Platform: Windows

MATLAB for Windows

Platform: Windows

Introduction

MATLAB (short for "MATrix LABoratory") is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment. MATLAB's strength is the ease with which it manipulates rectangular arrays of numbers (matrices), relieving the user of explicit dimensioning details.

Starting MATLAB

To start MATLAB, click start, select All Programs, MATLAB, R2007A, and then MATLAB.

start menu

When MATLAB opens notice the Command Window on the right.

command line

File Conventions

MATLAB expects certain file extensions when some commands are executed. Here is a list of file extensions:

File Contents Extension
MATLAB script file .m
MATLAB binary file .mat

MATLAB reads and executes commands from standard input, as well as from ASCII script files with .m extensions (called M-files, which can be created with the MATLAB built-in editor or Notebook). To execute the commands residing in a file named myproject.m, simply type the filename without the .m extension at a MATLAB prompt:
myproject

MATLAB executes each command from myproject until completion, and will then return you to a prompt.

Comments may be used to document your work in MATLAB. MATLAB treats any text following a percentage (%) symbol as a comment.

Help in MATLAB

MATLAB provides an effective online help facility, available by typing help. MATLAB responds by displaying a list of help topics, along with a brief description of each topic. You can get specific help by typing help with a topic name. For example, typing help graph3d lists the commands that relate to three-dimensional graphics; typing help surf provides detailed help on MATLAB's surface plotting command surf.

Online help is also available from the menu bar located at the top of the MATLAB window.

To get a quick introduction to MATLAB's features, run the demo program by typing demo.

MATLAB Command Syntax

MATLAB is case-sensitive. All built-in commands are in lowercase. A command normally terminates with a carriage return. Including a semicolon (;) at the end of a command suppresses MATLAB's echoing to the terminal. Typing b = 2*a; stores the result of 2*a in a variable, b, but does not display the result. (This is useful when dealing with large sets of numbers.)

Matrices

MATLAB works with essentially one kind of object: a matrix of numbers (which could include complex elements). Scalars are 1-by-1 matrices and vectors are 1-by-n or n-by-1 matrices.

When entering a matrix, separate columns by spaces or commas, and rows by semicolons. For example:
A = [1 2; 3 4]

The result is:

matrices

MATLAB stores the above 2-by-2 matrix into the variable, A, for later use. To retrieve a variable, simply type its name (e.g., A).

Matrix and Array Operations

Matrix operations are fundamental to MATLAB and are based on principles of linear algebra. Matrix multiplication, division, power, and transpose are denoted by symbols *, /, ^, and '.

Array operations refer to element-by-element arithmetic operations, rather than the usual linear algebraic operations. Array operations are denoted by preceding an operator with a period (.), such as .*,./ and .^. For addition and subtraction, array and matrix operations are the same.

See the Examples section for a comparison of matrix and array operations.

The Colon (:) Notation

The colon (:) is an important notation. It can be used to generate vectors and access submatrices. For example:
x=0 : pi/4 : pi

The result is:

colon

The combination of vectors can generate matrices. For example:
y=exp(-x) . *sin(x) ;
z=[x;y]

This produces:
z=0 0.7854 1.5708 2.3562 3.1416
    0 0.3224 0.2079 0.0670 0.0000

The colon notation allows you to access any portion of a matrix. For instance:
z(:,2:5)

This specifies the 2-by-4 submatrix that consists of the last four elements in all rows. Please note that using the colon alone denotes all of a corresponding row or column.

Editor/Debugger

MATLAB's built-in Editor/Debugger can be used to edit and debug command/script files. To access it, type:
edit

editor

Saving Your Work

Choose Save Workspace As... from the File menu to save your work into a .mat file. Files with a .mat extension are in binary format and are not readable. To open an existing .mat file, select Load Workspace from the File menu.

saving work

Printing

Select Print from the File menu to print a copy of your MATLAB session.

print

Exiting MATLAB

To end your MATLAB session, type exit or quit, or choose Exit MATLAB from the File menu.

exit

Examples

MATLAB's command language is expressive, extensible, and easy to use. Using MATLAB interactively by entering commands at its >> prompt is an effective way to learn.

The following commands help illustrate some of MATLAB's features:

Example Description
clear Clears all variables from memory.
a = [1 2; 3 4] Creates a simple 2x2 matrix.
b = inv(a) Inverts matrix a and stores it as variable b.
c = a * b Matrix multiplication of a and b.
c = a .* b Array operation (note the decimal point in the operator).
c(:, 2) The second column of matrix c.
c(2, :) The second row of matrix c.
who Lists the active variables now.
x = 0: pi/30 : 4*pi Creates a row vector whose elements range from 0 to 4 pi, in increments of pi/30.
x = x' Takes the transpose of x, and stores the resulting column vector as x.
y = exp (-0.3 * x) .* sin (x) Creates column vector y as a function of column vector x (note the .* operator).
plot(x,y) Plots the column vectors x and y.
title('Decaying Sinusoid') Creates a plot title.
xlabel('Time (sec)') Labels the x-axis.
ylabel('Position (in)') Labels the y-axis.
print -dps decay Creates a PostScript plot file decay.ps.
clf Clears the displayed figure.
[x, y] = meshgrid(-1: 0.05 :1 , -1: 0.1 : 1) Generates matrices x and y to support 3D surface plotting over the provided intervals.
z = sin(5 * x .* y); Creates matrix z, from x and y.
surf(x,y,z) Plots the surface z=sin(5xy).
view( [20 60] ) Changes the view and replots (azimuth & elevation).

Additional Information

For a complete description of MATLAB, refer to MATLAB User's Guide, published by The MathWorks, Inc.

To access online manuals, click on Help and then Help Desk.

Related Documents

Document Information

Title: MATLAB for Windows
Document Category: Windows
Document Number: WIN-027