Home

MATLAB For Dummies Cheat Sheet

|
|  Updated:  
2021-07-12 13:13:56
|   From The Book:  
MATLAB For Dummies
Explore Book
Buy On Amazon
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 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.
figure(<number>) Obtains the handle for the specified figure.
findobj('Type', 'figure') Obtains the handles for all existing figures.
format compact Removes extraneous spaces from the Command Window. Use the loose option to restore the default display. Other options include: short (default), long, short e, long e, short g, long g, short eng, and long eng.
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 the output appears simultaneously.
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

It’s important to know which operators MATLAB supports, and remembering them all isn’t always easy. The following table provides a quick summary of the operators that MATLAB supports.

Operator Type Description Example
Arithmetic Subtracts the right operand from 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 raised to the power of 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 versus matrix arithmetic.  There is 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 is 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 if all of the array elements are non-zero or true. all([1, 2, 3, 4]) = 1

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

any Logical Determines if any of the array elements are non-zero 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. —(@@nd4) results in 4 while —4 results in —4
+ Unary Provided purely for the sake of completeness. This operator returns the same value you provide as input. +4 results in a value of 4

 

Live Script Control Summary

Using Live Script controls can make your documents interactive. You can do everything from very simple things, like displaying a Help button, to providing controlled inputs for your algorithm. You can create input form documents or design a test. Using controls gives your application more of a full-fledged application feel without requiring you to get a computer science degree. Not that the experience of using controls is free of some development tricks, but using the MATLAB controls is easier than working with controls in a full-fledged development environment. MATLAB supports these controls:

Control Purpose Description
Button Lets you define the precise moment when an action should occur After providing all the input required to create a plot, the user clicks the Plot button in your application and sees the resulting plot onscreen. To use the Button control effectively, you must set the Run field for any controls in the document to Nothing.
Check Box Provides logical input with an output of one of two values The feature is either on or off, the answer is either yes or no, the user either wants to opt in or opt out.
Drop-Down List Contains a set number of input values The user selects the desired input value from the list, meaning that incorrect input values due to typos and other forms of user error are a thing of the past. Relying on drop-down list boxes instead of requiring the user to type values also makes data entry significantly faster.
Edit Field Provides a convenient means of allowing free-form input from the user Avoid using the Edit Field control when you can; it creates the potential for a security breach unless you perform a lot of background checks. The Edit Field control is the most flexible of all the controls because you really can perform free-form input.
Numeric Slider Allows control over a value within a specific range You might want to allow the viewer to choose the number of graphs displayed at any time as part of the plot. Moving the Numeric Slider control’s thumb (the square box that controls its value) to the desired number of graphs will change the appearance of the plot. The advantage of using a numeric slider is that you control the correct input values and don’t need to consider values outside the desired range, thereby reducing application errors.

 

Live Editor Coding Buttons

Specialized features can make your coding experience easier and possibly save time as well. MATLAB provides a set of six specialized coding buttons in the Code section of the Live Editor tab. The following table offers a quick summary of how you use each button (or associated speed key) to perform tasks in Live Editor.

Button Name Speed Key Purpose
Comment Ctrl+R Adds a % in front of each selected line in a group, commenting the code out for testing or debugging.
Uncomment Ctrl+T Removes the first % in front of each selected line in a group, making the code active again. If the group contains a comment line, only the first % is removed, which means that the comment stays a comment.
Wrap Comments Ctrl+J Combines lines with a similar indent to make a comment block smaller. Works only with lines that are already commented out.
Smart Indent Ctrl+I Adds indentation as needed to make the code more readable. MATLAB uses natural code boundaries, such as if…else statements, to make the indentation.
Increase Indent Ctrl+] Adds a single level of indentation to the selected lines of code.
Decrease Indent Ctrl+[ Removes a single level of indentation from the selected lines of code. If the selected code is already at the left margin, the button has no effect on that line.

 

Line Plot Styles

Whenever you create a plot, 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 Five-point star
h Six-point star

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

About This Article

This article is from the book: 

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.