Information Technology Services - Computing, Networking and Storage

MATLAB




Introduction to MATLAB

MATLAB is a high performance software package for numerical computation and visualization. The name MATLAB stands for Matrix Laboratory. The basic MATLAB operating structure is a matrix, and the program is designed especially to help compute problems with matrix and vector formulations. The program lets users manipulate numerical data and create graphical representations of the information. MATLAB also offers a complete programming environment that is serves as a powerful tool for numerical applications.

Additionally, there are several MATLAB toolboxes available for use. MATLAB toolboxes are sets of MATLAB programs designed to solve a specific set or type of problem.

What you will need

  • A UNIX workstation with a windowing environment, such as CDE, or X-Windows. For more information on X-Windows, see the X-Windows Server documentation page.

  • An active USC computer account on one of the ITS time-sharing hosts (e.g. SCF, RCF, HSC) or any host whose accounts are managed by ITS (e.g. pollux, chem1). More information on USC computer accounts can be found on the Computer Accounts page.

You will also need to source the MATLAB setup files in order for the software to work properly. You may source the setup files by typing a command at the UNIX prompt before you need to use MATLAB, or if you wish to have the MATLAB setup sourced each time you login, you may add a command to your .login file.

To source the setup files at a UNIX prompt use the command:

	source /usr/usc/matlab/default/setup.csh

To source the setup files at login, add the following lines to your .login file:

 
	if ( -e /usr/usc/matlab/default/setup.csh) then
	   source /usr/usc/matlab/default/setup.csh
	endif

Starting MATLAB

Once you have logged into a windowing environment and have sourced the MATLAB setup files, you may start up MATLAB by typing matlab at the UNIX prompt. Once MATLAB has started up, you will see the MATLAB desktop. The desktop consists of 3 windows: Workspace, Command History, and Command Window. The Command Window is where you will see a prompt that looks like >>, and is where you will type in various commands.

Basic MATLAB Commands

At the MATLAB prompt, you can run some basic UNIX commands such as cd and ls. For more information on UNIX commands, see the UNIX Documentation Page.

To get general MATLAB help, type help at the prompt to get a list of a help topics. To get help on a specific topic or function, type help <topicname> at the prompt, replacing <topicname> with the topic or function you wish to get help on.

To exit MATLAB, type exit or quit at the prompt.

Matlab Basics

MATLAB is designed to work with matrices, where a matrix is defined to be a rectangular array of numbers. All variables used are considered to be matrices. Scalars and vectors can be used since they can be considered as matrices with dimension 1x1 (scalars) and 1xn or nx1 (vectors).

Unlike programming languages such as C or Java, MATLAB does not require any type declarations or dimension statements. When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage. If the variable already exists, MATLAB changes its contents and, if necessary, allocates new storage. To check to see what variables already exist and what dimensions they are, type the command whos at the prompt. To clear existing variables from memory enter the command clear at the prompt.

To create a variable, simply type the variable name at the prompt, followed the the equals sign (=), and followed by the initialization, as demonstrated below:

	     To initialize a scalar a:
		>>a=[2]

	     To initialize a row vector b:
	        >>b=[1 2 3 4 5]
	     Note that spaces between numbers signify a new column.

	     To initialize a column vector c:
		>>c=[6; 7; 8; 9]
	     Note that semicolons between numbers signify a new row.

	     To initialize a 3 by 3 matrix M:
		>>M=[1 2 3; 4 5 6; 7 8 9]
	     Again, spaces between numbers signify a new column,
	     whereas semicolons signify a new row.
	

Note: Although not required by MATLAB, linear algebra conventions of naming matrices with capital letters and scalars/vectors with lower case letters is often retained for readability inside MATLAB.

Generating Matrices

MATLAB provides four functions that allow you to easily generate basic matrices.

  • The zeros function creates a matrix with all elements equal to zero. For example, to create a 3 by 4 zero matrix Z:

    	     >>Z= zeros(3, 4)

  • The ones function creates a matrix with all elements equal to one. For example, to create a 2 by 3 ones matrix O:

    	     >>O= ones(2, 3)

  • The rand function creates a matrix with uniformly distributed random elements. For example, to create a 4 by 1 random matrix R:

    	     >>R= rand(4, 1)

  • The randn function creates a matrix with normally distributed random elements. For example, to create a 2 by 5 random matrix R:

    	     >>R= randn(2, 5)

Matrix Operations

MATLAB provides several useful matrix operations:

    + Matrix addition
    - Matrix subtraction
    * Dot product
    ' Takes the transpose of a matrix
    inv Takes the inverse of a matrix
    eig Computes the eigenvalues of a square matrix
    det Computes the determinant of a matrix
    rref Calculates the reduced row echelon form of a matrix

Using M-Files

MATLAB allows you to write scripts and define functions in external files called M-Files. M-Files have the file extension .m, and can be executed at the MATLAB prompt by simplying typing in the filename without the .m extension. To create a new M-File:

  • Go to the File menu, go to the New submenu, and select M-file.

  • To make comments in the M-file, type a percent sign (%) at the beginning of the line you wish to be commented.

  • To save the M-file, go to the File menu, and select Save As... and select the location in your home directory that you wish to save the file to.

  • To close the M-file, go to the File menu, and select Close.

MATLAB Toolboxes

At USC, the following MATLAB toolboxes are available:

  • Control Systems

  • Fuzzy Logic

  • Image Processing

  • LMI Control

  • Neural Networks

  • Optimization

  • Partial Differential Equation

  • Robust Control

  • Signal Processing

  • Statistics

  • Symbolic Math

  • System Identification

  • Wavelets

To access a Toolbox, go to the MATLAB Start menu, then go to the Toolboxes submenu, then select the Toolbox you wish to use.

Getting Help With MATLAB

To get general MATLAB help, type help at the MATLAB command prompt to get a list of a help topics. To get help on a specific topic or function, type help <topicname> at the prompt, replacing <topicname> with the topic or function you wish to get help on.

MathWorks provides extensive MATLAB documentation online. To access this documentation, go to MATLAB Getting Started

If you have questions regarding this documentation, or are having trouble logging into your account and sourcing the MATLAB setup files, please contact the Customer Support Center at (213)740-5555 or e-mail consult@usc.edu.


Last Updated: Friday, June 02, 2006 at 12:07PM PDT