MATLAB For Dummies Cheat Sheet - dummies
Cheat Sheet

MATLAB For Dummies Cheat Sheet

From MATLAB For Dummies

By Jim Sizemore, John Paul Mueller

MATLAB is an incredibly flexible environment that you can use to perform all sorts of math tasks. A large array of engineering and science disciplines can use MATLAB to meet specific needs in their environment. Using such a complex environment can prove daunting at first, but this Cheat Sheet can help: Get to know common MATLAB commands; become familiar with common operators and precedence; and learn to recognize line plot styles.

Common MATLAB Commands

The following table contains a listing of commands that you use relatively often in MATLAB. You won’t find every command listed — that would require a book in itself. However, these commands are usually used several times each session.

Command Purpose
cla Clears the current plot
clc Clears the Command window
clear <variable name> Removes a specific variable from the Workspace window (as
specified by <variable name>)
clear all Removes all of the variables from the Workspace window
close <figure name> Closes a specific figure (as specified by <figure
name
>)
close all Closes all of the current figures
diary <filename> Specifies the name of the file to use for the Diary
feature
diary off Stops saving the Command window text to a file
diary on Starts saving the Command window text to a file
exist <keyword> Checks whether a keyword or file is in use
format compact Removes extraneous spaces from the Command window
gca Obtains a handle to the current axes
gcf Obtains a handle to the current figure
gco Obtains a handle to the current object
get(<handle>,
<
property>)
Obtains the <property>
found in the object pointed at by <handle>
help <command or
file>
Displays help documentation for the <command> or
comments in files you’ve created
iskeyword Displays a list of all the MATLAB keywords
iskeyword <name> Determines whether <name> is a
keyword
load <filename> Loads the file containing variables to the Workspace
window
more off Displays output using standard scrolling so that all of the
output appears at one time
more on Tells MATLAB to display output one screen at a time
save <filename> Saves the variables shown in the Workspace window to the
specified file
set(<handle>,
<
property>, <value>)
Sets the <property>
found in the object pointed at by <handle> to
the specified <value>

MATLAB Common Operator Summary

You need to know which operators MATLAB supports, but remember them all isn’t easy. The following table provides a brief summary of the operators that MATLAB supports.

Operator Type Description Example
Arithmetic Subtracts the right operand from the left operand. 5 – 2 = 3
* Arithmetic Multiplies the right operand by the left operand. 5 * 2 = 10
^ Arithmetic Calculates the exponential value of the right operand by the
left operand.
5^2 = 25
/ Arithmetic Divides the left operand by the right operand. 5 / 2 = 2.5000
Arithmetic Divides the right operand by the left operand. 5 2 = 0.4000
+ Arithmetic Adds two values together. 5 + 2 = 7
. Arithmetic Modifies operators to perform element-by-element arithmetic
vis-à-vis matrix arithmetic. You receive no modification if
you’re operating on scalars (ordinary numbers).
[1,2]*[3;4] = 11

[1,2].*[3,4] = [3,8]

= Assignment Assigns the value found in the right operand to the left
operand.
MyVar = 2 results in MyVar containing
2
bitand Bitwise Performs a logical and of the bits in
two numbers.
bitand(4, 5) = 4
bitor Bitwise Performs a logical or of the bits in
two numbers.
bitor(4, 5) = 5
bitget Bitwise Obtains the value of the bit at a specific location. bitget(4, 3) = 1
bitset Bitwise Changes the bit at the specified location. bitset(4, 1, 1) = 5
bitshift Bitwise Shifts the bits the specified number of positions. bitshift(2, 1) = 4
bitxor Bitwise Performs a logical exclusive or on
the bits in two numbers.
bitxor(4, 5) = 1
and Logical Determines whether both operands are true. and(true, true) = 1 (or true)

and(true, false) = 0 (or
false)

and(false, false) = 0

and(false, true) = 0

not Logical Negates the truth value of a single operand. A true value becomes false and a false
value becomes true.
not(true) = 0

not(false)=1

or Logical Determines when one of two operands are true. or(true, true) = 1

or(true, false) = 1

or(false, false) = 0

or(false, true) = 1

xor Logical Determines when one and only one of the operands is true. xor(true, true) = 0

xor(true, false) = 1

xor(false, false) = 0

xor(false, true) = 1

all Logical Determines whether all the array elements are nonzero or
true.
all([1, 2, 3, 4]) = 1

all([0, 1, 2, 3]) = 0

any Logical Determines whether any of the array elements are nonzero or
true.
any([0, 1, 0, 0]) = 1

any([0, 0, 0, 0]) = 0

~= Relational Determines whether two values are not equal. 1 ~= 2 is 1 (or true)
< Relational Verifies that the left operand value is less than the right
operand value.
1 < 2 is 1
<= Relational Verifies that the left operand value is less than or equal to
the right operand value.
1 <= 2 is 1
== Relational Determines whether two values are equal. Notice that the
relational operator uses two equals signs. A mistake many
developers make is using just one equals sign, which results in one
value being assigned to another.
1 == 2 is 0
> Relational Verifies that the left operand value is greater than the right
operand value.
1 > 2 is 0
>= Relational Verifies that the left operand value is greater than or equal
to the right operand value.
1 >= 2 is 0
Unary Negates the original value so that positive becomes negative
and vice versa.
-(-4) results in 4 while -4 results in
-4
+ Unary Provided purely for the sake of completeness. This operator
returns the same value that you provide as input.
+4 results in a value of 4

MATLAB Operator Precedence

Knowing the order in which tasks are performed is essential. Otherwise, the formulas you type won’t work as expected and you’ll obtain errant results. The following table shows the order in which MATLAB evaluates various operators.

You can also remember operator precedence using the PEMDAS acronym, which stands for Parentheses, Exponent, Multiply And Divide, Add and Subtract.

Operator Description
() Parentheses are used to group expressions and to override the
default precedence so that you can force an operation of lower
precedence (such as addition) to take precedence over an operation
of higher precedence (such as multiplication).
. .^ ^ Transpose, power, complex conjugate transpose, matrix
power.
+ – ~ Unary operators interact with a single variable or
expression.
.* ./ . * / Multiplication and division(both right and left).
+ – Addition and subtraction.
: Colon operator (used for ranges).
<= < > >= Comparison operators.
== ~= Equality operators.
& | Logical operators (element-wise).
&& || Logical operators (short-circuit).

Line Plot Styles in MATLAB

Whenever you create a plot in MATLAB, you need to identify the sources of information using more than just the lines. Creating a plot that uses differing line types and data point symbols makes the plot much easier for other people to use. The following table contains a listing of the line plot styles.

           Color                         Marker                              Style
Code Line Color Code Marker Style Code Line Style
b blue . point Solid
g green o circle : Dotted
r red x x-mark -. dash dot
c cyan + plus Dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v down triangle
^ up triangle
< left triangle
> right triangle
p 5-point star
h 6-point star

Remember that you can also use these styles with other kinds of plots. For example, a scatter plot can use these styles to define each of the data points. When in doubt, try the styles to see whether they work with your particular plot.