(Earlier versions of this page were titled MATLAB as a programming language)
Apr -- Aug 2006 begun, unfinished topics end with
Mar 2007 fill in some topics, minor tweaks, June 2007 fill in biquad
example, fix matlab-numpy link, Mar 2008 Separate Commands section,
add plot commands.
I recently learned MATLAB to design and analyze digital filters for a project (also here and here).
You can get a lot of work done in MATLAB without programming. Nevertheless, its programming language has some unusual features, particularly regarding matrices and persistence. It also provides most of the facilities of a conventional language, so you can program in a familiar style if you wish, but you can often do much better if you take advantage of its unique features.
The MATLAB programming language and IDE support an ad-hoc programming style where it is easy to turn the useful parts of an interactive session into a program.
This page is not intended to be a reference or even a summary of MATLAB, for that see the references. Instead, I point out some important differences from other languages, that I expect will be interesting to programmers and useful for newcomers.
REPL, Commands, Program structure, Data types, Matrices, Exceptions, Persistence, Large systems, Alternatives, References
Invoking MATLAB usually starts a read-evaluate-print loop (REPL).
You type an expression, statement, or command at the command prompt
>>, and MATLAB prints the result (if there is one), or
executes some side effect such as displaying a graph.
Invoking MATLAB usually starts an Integrated Development Environment (IDE) that includes a window for the REPL, editor windows for writing code, a command history window, a file system browser etc.
Random mouse motions can cause the panels in
the IDE to scatter like leaves before a storm. To restore them,
use the main panel menu bar:
Desktop -> Desktop Layout -> Default.
The REPL makes it easy to do ad-hoc calculations and plotting. The MATLAB programming language and IDE make it easy to turn the useful parts of a REPL session into a program.
You can also run MATLAB without the IDE in a terminal window.
|Print command line options|
||Run in terminal with no IDE|
||Start MATLAB, execute |
You create a variable just by assigning to it. There are no variable declarations. You can assign values of different types to the same variable.
Be warned that MATLAB will happily and silently allow you to reassign
variables that it initially assigns, such as
i. I haven't found any way to define a constant.
Scripts and functions in the filesystem are loaded implicitly, just by
naming them. There is no command to load or import code.
load command is used for persistence.
... at the end of a line continues the command
on the next line, like backslash
\ in the Unix shells or
An optional semicolon
; at the end of a command is not a
a terminator or separator. Instead, it suppresses printing the
result. You often want this, especially when the result is some huge
Identifiers are case-sensitive.
A is different from
a, etc. It is confusing that
shows function and command names in capitals. If you type them in that
way, they will not work.
Array indexing begins at 1, not 0 as in most programming languages.
Use single quotes for strings:
You can't use a hyphen
- in an indentifier. MATLAB thinks
it's a minus sign. Use an underscore
ans stores the value computed by the last
command (the "answer"), so you can use it in the next one.
You can use wild cards in variable names in commands where that makes
sense, for example
whos x* and
Here are some useful commands for interacting with the REPL. There is a much larger command summary here.
Many commands have several variants with the same name but different
argument lists and return values, so it is a good idea to consult
help about any command you use.
||Print information about version and license, including all installed toolboxes|
||Print information about
||Print full path to source file for
||Print source code for
||Print list of commands whose name or brief description contains
||(or any other operator) print information about all operators and special characters.|
||Print fewer blank lines, to fit more on the screen.|
||Print lots of digits in results,
||Assign vector of function values to variable
||List all user-defined variables|
||List all user-defined variables, along with their sizes and types ("classes")|
||Remove all user-defined variables, to avoid confusion from previously assigned values.|
||Plot values of
||Plot values of
||Plot values of
||Create new figure to show output of subsequent plot commands, to preserve previous figure.|
||Make subsequent commands plot on the same figure, without clearing plots already displayed there.|
||Create new figure, return figure handle h.|
||Make figure h the current figure (or just bring figure h to the top using the window manager).|
||Delete figure h.|
||Delete all figures, to unclutter the screen.|
||Execute command with system command interpreter (not MATLAB).|
M-files, Scripts, Functions
MATLAB source code must be stored in files with the
extension, called M-files.
Every M-file defines one MATLAB command (a statement or a function).
The name of the command is the file name (without the
extension). When you invoke a command (at the REPL or in another
M-file), MATLAB searches a path for an M-file with that name, and
executes it. There is no separate command to import or load an M-file.
You can't use a hyphen
- in the name of an M-file. MATLAB thinks
it's a minus sign. Use an underscore
It is the convention that the first comment line in
command.m has the form
%COMMAND one-line description of command ...
%BIQUAD make cascaded biquad filter, return coeffs for 2nd order sections
The IDE file browser displays this line, and some REPL commands use it:
||Print first block of comment lines in
||Print first comment line in each M-file whose name contains
Functions local to an M-file with
There are two kinds of M-files, scripts and functions.
A script defines a command with no arguments and no return values. It is just a sequence of MATLAB commands (and comments). Invoking a script executes those commands, exactly as if they were typed at the REPL (or invoked in the script that invoked the command).
All variables in a script are global; they can be used and assigned in the REPL session (or script) that invoked the script.
MATLAB encourages an ad-hoc programming style where you first experiment by typing commands at the REPL, then create a script by saving the sequence of commands that worked. The MATLAB IDE supports this by providing a command history window, from which commands can be cut and pasted into an editor window.
A function defines a command that may take arguments and return values. Here are the first few lines of biquad.m:
function [Hsosm,sosm] = biquad(bd,ad,frac_bits,gain) %BIQUAD make cascaded biquad filter, return coeffs for 2nd order sections % ... other comment lines describe inputs, outputs, etc. ... H = dfilt.df2(bd,ad); Hsos = H.sos; sosm = Hsos.sosmatrix; ... more code follows ...
The name of the function must be the same as the file name. Notice that there are no data types mentioned in the declaration.
Functions can return multiple values, as in the previous example. That function could be called like this:
>> bd = ... >> ad = ... >> [Hsosm,sosm] = biquad(bd, ad, 14, 1.0)
The brackets on the left hand side of this assignment might suggest
that there is a tuple data type, but apparently there is no such
thing; this is special syntax for function return values only. In
[n,x,s] = ..., where the right hand side is not
a function call, does not work.
There is no
return statement. After the last statement
in the function executes, the function exits and the values of all the
variables whose names appear on the left side of the function
declaration are returned.
Variables in a function are local.
MATLAB encourages an ad-hoc programming style where it is easy to turn a script (or part of a REPL session) into a function. Simply add a declaration at the top that defines the name and identifies which variables are the parameters and which store the return values.
You can ignore returned values. It is not an error to assign fewer variables than the function returns, or to assign no variables at all.
Functions can have optional arguments. Within each function you can
nargin, the number of arguments the function was called
with, for example biquad.m (above) contains
... if nargin < 3 frac_bits = 14; % default end ...
There are no keyword arguments. MATLAB functions often achieve the same effect by using pairs of optional arguments, where the first of each pair is a string (the keyword), and the second is the value.
Function arguments with
earlier scheme with strings
Function names can be overloaded, with different parameters and return values
requires care. The problem seems to be that the
statement is not vectorized - apparently
if always treats the
conditional expression as a scalar. If matrix elements are subject to
conditionals, so that different elements might take different paths, this
is the one circumstance where you must iterate.
MATLAB calls data types "classes". The
shows the classes of all of your variables.
There are no scalar classes. Scalars are a special case of matrices: matrices with one element.
Numbers, Booleans, Strings, Objects
All numbers are stored as double-precision floating point values. Usually, many more digits are stored than are printed.
fix return the integer nearest
to a number by various criteria.
Complex numbers are entered (and printed) as in
1 + 2i
1 + 2j). Extract the components of a complex number
with the functions
imag, find the
magnitude and phase with
j are initially assigned
(by MATLAB) to the imaginary constant
0 + 1i. This can
be confusing because you (or scripts you use) often assign them to
something else. Use
There is an initially assigned variable
pi but not
There is no Boolean type. MATLAB documentation refers to the logical (not Boolean) values 0 (false) and 1 (true), which are returned by logical operations.
Any nonzero value is interpreted as true when it appears as an operand of a logical operation.
MATLAB is not known for text processing, but it has a string data
type. The command
lookfor string shows what functions
Use single quotes for strings:
Strings are used in printed output, and to put labels and captions on graphs.
sprintf functions like
in C, with the usual format strings, escape characters, etc. However,
if there is an array argument, the corresponding format string is
applied to every element, printing out the whole array.
Printing large arrays, with control over the layout of rows and columns, is one of the rare MATLAB operations where I have found it helpful to code loops.
Strings are often used as symbols. Many functions use particular
strings as arguments to select options. For example, the
gensig signal generator function uses the strings
to select the waveform type.
Strings are actually 1-dimensional arrays of characters, characters are 1-element arrays. All the usual matrix operators apply.
Objects and dot notation
Examples from filter toolbox
Vectorization, rows vs. columns, constructors, accessors, assignment
An array is called a matrix. A one-dimensional array is a vector. Matrices and vectors are central in MATLAB; most of the unusual features in the language concern them. They are very easy to work with. It is rarely necessary to code loops in order to use them, as we shall see.
MATLAB itself uses arrays wherever it can. Strings are vectors of
characters. The ranges in iterations are arrays. In a loop that
for x = v ..., the index variable
iterates over the elements of vector
for x =
x iterates over the columns in the matrix
A. (But loops appear only rarely in MATLAB programs).
Many operators and functions are vectorized: they work on
every element in a vector or matrix. It is not necessary to use any
"map" function to achieve this. For example, assign
sin(x) evaluates to
1, as usual.
But also assign
v = [ 0 pi/6 pi/2 ], then
sin(v) evaluates to
[ 0 0.5 1.0 ].
Some MATLAB statements are also vectorized. The statement
fprintf('%8.3f', A) prints every element in the matrix
A with the specified format.
Some binary operators are applied element-by-element in the
obvious way. For example, if
matrices, the sum
A+B is the matrix of their
element-by-element sums. This requires that
B have the same number of dimensions and the same size in
each dimension. Otherwise, this expression raises the error
Matrix dimensions must agree.
Operators that work element-by-element are sometimes called element-wise operators, pointwise operators, or array operators.
Some binary operators are not simply applied element-by-element. Instead
they have their traditional meaning from mathematics. For example,
* is matrix multiplication and
^ is matrix
power. These are called matrix operators to distinguish them
from the element-by-element array operators.
For every matrix operator, there is also a corresponding element-wise array
operator. For example, the array operators for
multiplication and power are
(notice the leading dot).
When one of the operands is a scalar, you do not have to use the array
operator. For scalar
c+A etc.) computes the products (or sums etc.)
element-wise. In this context, MATLAB treats
c as a
mathematical scalar, not a one-element matrix (which would have the
wrong size for this matrix multiplication).
Some functions are reductions that compute a scalar from a
vector. They include
std (standard deviation). Reductions applied to 2-D
matrices return a vector of the results of applying the reduction to
Relational and logical operators can also take matrix operands. For
A > 10 or
A > B etc. evaluates the
Boolean expression element-wise, and returns the matrix with the
1 (true) at the elements where the
expression is true, and
0 (false) elsewhere.
The Boolean reductions
any act as logical quantifiers; they return 1 or 0 (true
or false) to indicate whether all or any of the elements are nonzero (true).
find(A) returns the vector of linear indices of
nonzero (true) elements in
10) etc. returns the vector of indices where the Boolean
expression is true. The vector of indices can then be used to access or update those
help * (or any other operator symbol besides
*) prints information about all the matrix and array
Vectorization, array and matrix operators, and reductions contribute to making loops mostly unnecessary.
Scalars are matrices. Every scalar can be treated as a matrix of one
element, with as many dimensions as needed in context. For example,
a = 42, then evaluate
a(1,1,1) etc. Therefore, no special
operator or function is needed to extract a scalar from a one-element
matrix, or to concatenate a scalar to a vector.
There are two kinds of one-dimensional arrays. Like mathematics but unlike most programming languages, MATLAB distinguishes between row vectors and column vectors. Built-in functions and toolboxes use the conventions of the application domain. For example, time series are column vectors, polynomial coefficients (including digital filter coefficients) are row vectors, and polynomial roots are column vectors.
Even vectors are considered to have two dimensions: row vectors are 1
x N matrices, column vectors are N x 1 matrices, for example try
v = [ 1 2 3 ] then
The distinction between row and column vectors is significant for some
operators and statements. For example, given the 1 x N row vector
vr and the N x 1 column vector
vr*vc is the inner product or dot
product, a scalar, but
vc*vr is the outer
product, an N x N matrix.
The matrix transpose operator, apostrophe
', creates a row
vector from a column vector and vice versa. Given row vector
vr, the assignment
vc = vr' creates column
The function application
ndims(A) returns the number of
dimensions in matrix
the size of each dimension, in an array of length
ndims(A). The reduction
returns the total number of elements. The index of the first element
in each dimension is
1 and the index of the last element
Construct row vectors using spaces or commas:
vr = [ a b c
vr = [ a, b, c ]. Construct column vectors
vc = [ x; y; z ]. Combine both to
A = [ a11 a12; a21 a22 ].
The entries in matrix constructors can be expressions with scalar or matrix values. You can use these constructors to build up large matrices, concatenate matrices, append scalars to vectors, etc.
zeros (note spelling), and
random construct matrices where all the elements are
0, or values drawn from a specified
random distribution. Consult
help about specifying
sizes, etc. These functions can be used to pre-allocate arrays in
order to use storage efficiently.
Range expressions (using the range operator, colon
construct vectors, given an initial value and a final value:
1:10. Range expressions can include an optional increment:
v = 1:2:10 and can use fractional values:
0.3:-0.15:-0.45. Range expressions are frequently used to code
for i = 1:n ....
The variety of constructors contributes to making loops mostly unnecessary.
Use subscripts to access scalar matrix elements:
Indexing begins at 1, not 0 as in most programming languages.
The index of the last element in a given dimension is
You can use it in expressions:
v(end - 1) etc.
Use ranges to access sub-matrices:
B = A(k:l,m:n).
Subscripts and ranges can be built from any integer-valued expressions. When ranges are used as accessors, they cannot include fractional values.
A range operator without operands ranges over all the accessible
vr = A(3,:) accesses the entire third row.
: is an abbreviation for
:j is not allowed).
A vector of indices
v([i,j,k]) returns the vector of
elements at those indices, the same as
[ v(i) v(j) v(k)
]. (Using ranges as accessors is a special case of this).
Recall that the
find function returns a vector of indices.
Use a single linear index as in
A(:) to access any matrix one
element at a time, as if it were a vector, with the elements indexed
by row first (scanning down columns), so
A(:) is the entire matrix flattened to a
The variety of accessors contributes to making loops mostly unnecessary.
Matrices are mutable. You can assign new values to individual
elements, sub-matrices, and selected elements by using indices,
ranges, or vectors of indices:
A(i,j) = x, or
A(i,:) = v, or
A([i,j,k]) = [ x y z ] etc.
Matrices are mutable, but most matrix operations return a new matrix,
rather than updating their operands in place. For example,
c*A etc. does not change
A. This makes it
easy to program in a functional style.
Assigning a matrix to a variable creates a new copy of a matrix; it
does not create an alias. The copy can be updated without changing
the original: after
A = [ 1 2 3 ] then
B(2)=4, the matrices
A differ. This is unlike many programming languages,
where assigning an array to a variable creates an alias, and updating
B would change both.
You can grow a matrix by appending in a constructor, or assigning to new elements (with higher indices) but that can be quite slow. It is much faster to pre-allocate storage by initially constructing a matrix of the needed size, if that is known.
You can easily save and restore the state of MATLAB session: the variables and their values.
You can save and restore individual matrices
load, store, dlmwrite ...
Warnings that print messages vs. errors that stop execution
open-source MATLAB clone. How complete?
IDL, PDL, etc. have MATLAB-like array operations
Command summary (NumPy also)
I learned what I know about MATLAB by reading (a little) in these sources, in roughly this order.
James H. McClellan, Ronald W. Shafer, and Mark A. Yoder, DSP First: a Multimedia Approach, Prentice Hall 1998 (especially, but not only, Appendices B and C.1)
Sze M. Tan, A Computational Toolbox for Quantum and Atomic Optics
Some useful Matlab tips (some tips but mostly links)
Getting Started with MATLAB
Signal Processing Toolbox User's Guide
Daniel T. Kaplan, Introduction to Scientific Computation and Programming, Brooks/Cole 2004 (teaches MATLAB as a general-purpose programming language)
Matlab tips and tricks (haven't read this yet, but looks useful)
Mathworks online documentation
http://www.mathworks.com/company/newsletters/digest/pastissues/index04_06.html, for example sept01/matrix.html