# University of Southern California

## ITS Information Technology Services

### 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 at the prompt, replacing 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 Operatons

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 at the prompt, replacing 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 email consult@usc.edu.

Last updated:
February 04, 2011

# MATLAB

###### In This Section
The use of all USC computing resources is governed by the USC Computing Policies.