INTRODUCTION TO MATLAB
3.1 Manipulating Matrices
The symbols for basic arithmetic operations with matrices are:
+ for addition —e.g.,
- for subtraction —e.g.,
* for multiplication —e.g.,
for inverting a matrix:
/ or \ for division—e.g., or G\C for “right” division or “left” division
Note that matrices must be conformable for these operations to be defined. Also note that C/G
and that , which are generally not the same.
Other useful operators include:
’ for transposition—e.g.,
for the determinant—e.g.,
for the trace—e.g.,
for the eigenvalues—e.g.,
MATLAB can easily join matrices together to make a larger matrix:
3.1.2 Element-by-Element Operations
Sometimes it’s useful to have MATLAB perform an operation on each element of a matrix. For
MATLAB can also perform element-by-element multiplication—e.g., – and division—
e.g., ( or equivalently , ).
3.2 Solving Linear Equations
X = A\B: Denotes the solution to the matrix equation AX = B.
X = B/A: Denotes the solution to the matrix equation XA = B.
4. Writing Simple Programs
The capabilities of Matlab can be extended through programs written in its own programming
language. It provides the standard constructs, such as loops and conditionals; these constructs
can be used interactively to reduce the tedium of repetitive tasks, or collected in programs stored
in (nothing more than a text file with extension ).
4.1 Script M-files
If you just want to enter in some simple problems for MATLAB to solve , using the Command
window is fast and easy. But you will often have a long sequence of commands on many
variables for MATLAB to evaluate—sometimes for hundreds of repetitions! MATLAB allows
you to type your commands in a text file, called a script or M-File, and then have the commands
in the M-file evaluated just as if they were entered in the Command window.
To create an M-File, click on the New M-file icon on the MATLAB desktop toolbar, or choose
New/M-File from the File menu. You can use the text window that appears to enter commands.
Now click the Run icon on the toolbar to execute your commands. MATLAB asks you to save
your file before it is run; use the Save file as: dialog box to name and save your file. In the
CSSCR lab, save your M-File to the /temp folder. (MATLAB may open a dialog box asking
about your Current Directory, if so, select the option that changes your Current Directory to the
/temp folder where you’ve saved your M-File.)
The results of running your M-File appear in the MATLAB command window.
4.2 For Loops
A For Loop executes a set of commands a given number of times . Try running this simple
program from an M-File:
4.3 While Loops
A While Loop executes a set of commands repeatedly, until a controlling expression is no longer
4.4 If-Else-End Conditional
The If-Else-End construct evaluates a logical expression and executes a command, or group of
commands , based on the value of that expression. Try running this simple program from an M-File:
These constructs can be used together, or nested within themselves and/or one another, allowing
you to write powerful programs in which the results of past calculations affect subsequent
4.5 The Current Directory and Search Path
To use an M-File that you have created, MATLAB needs to know where to find it. MATLAB
looks on a Search Path so you need to make sure that the directory in which you saved your MFile
is on this path. The easiest way to do this is to make the directory in which you’ve saved
your M-File the Current Directory.
Alternatively, you can put that directory on the Search Path by selecting Set Path in File menu
or by typing the following in the Command window:
For example in the Econ grad computer lab you would type
when logged on with your userid.
4.6 Function M-Files
These M-Files differ from the script M-Files you’ve been working with in that they accept input
arguments and return output arguments. A Function M-File operates on variables contained
within its own workspace, which is separate from the workspace you’ve been accessing from the
command line or through script M-Files. To write a function M-File, open a new M-File and
type the following:
Now save your function M-File—you must save your file with
exactly the same name you gave
the function—test1.m in this example (make sure you save it to either the current directory or a
directory that is on the search path). In the command window, enter any conformable “a” and
“B” (note: you do not need to name them “a” and “B”—the function will operate on any
conformable inputs that you give it). In the command window type the following:
Now call your function by typing
5. Plotting Graphs
MATLAB has powerful graphing features . To get started plotting graphs, try this example of a
simple 2-D graph.
When the graph appears, use the menu and toolbar to modify it and/or copy and paste it into a
6. Working With Data
You will certainly need to export your results from MATLAB or import data from other sources.
Let’s start by saving some of what we did until now. The basic command format is the save
command followed by the name of the file to create. This is followed by a list of the variables
which are to be saved in the file. To create an ASCII file which can be read by a spreadsheet the
list of variables is followed by the command –ascii.
This saves the A, B, and C variables in a MATLAB data
file (with .mat extension) in the current directory.
This saves all variables in the Workspace
in a ASCII file in the directory C.
Now let’s clear all variables in the workspace:
Loading data from a .mat file is easy. Just use instead of the command .
Note that you can choose which variables to load. This command loads only A and B, if you
wanted to load all variables you could simply write “load exampleFile”.
A quick method of importing text or binary data from a file (e.g., Excel files) is to use the
MATLAB Import Wizard. Open the Import Wizard by selecting File -> Import Data at the
Specify or browse for the file containing the data you want to import and you will see a preview
of what the file contains. Select the data you want and click Finish. (For more information, see
Help file for ‘Importing Text Data’)