Show Menu
Cheatography

MATLAB Cheat Sheet (DRAFT) by

A cheatcheet with all the MATLAB essentials.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Fundam­entals

Clear command window
clc
Clear all variables
clear
Clear specific variable
clear <va­ria­ble>
Get standard docume­ntation
help <co­mma­nd>
Get detailed docume­ntation
doc <co­mma­nd>
One line comment
% <Te­xt>
Print to command window
fprint­f('­<Te­xt>')
Continue code on next line
...
(Equiv­alent to space, don't split up 'Text')

Links for Further Inform­ation

 

Mathem­atical Constants and Functions

π
pi
e
exp(1)
inf
Expone­ntial function
exp(x)
Square root
sqrt(x)
Sine
sin(x)
Cosine
cos(x)
Tangent
tan(x)

Operators

Addition
+
Subtra­ction
-
Multip­lic­ation
*
Division
/
Left division
\ 
(a\b = b/a)
Expone­nti­ation
^

Matrix and Vector Operations

Elemen­t-wise multip­lic­ation
.*
Elemen­t-wise division
./
Elemen­t-wise expone­nti­ation
.^
Transpose
.'
Dot product
dot(A,B, dim)
Cross product
cross(A, B, dim)

Format Specifiers

Single character
%c
Decimal notation (signed)
%d
Decimal notation (unsigned)
%u
Expone­ntial notation
%e
Fixed-­point notation
%f
String or char array
%s
 

Creating and accessing Matrices and Vectors

Create m by n matrix filled with ones
X = ones(m , n)
Create m by n zero matrix
X = zeros(m, n)
Create m by n identity matrix
X = eye(m, n)
Create row vector
X = [1 2 3] or X
= [1, 2, 3]
Create column vector
X = [1; 2; 3]
Create matrix
X = [1, 2, 3; 4, 5, 6]
Create a vector with consec­utive numbers
X = 1 : 5 
(Start : End)
Create a vector with consec­utive numbers in a specific interval
X = 0 : 0.1 : 1
(Start : Interval : End)

Plotting

Plot values
plot(X, Y)
Plot values and set formatting
plot(X, Y, LineSpec)
Plot new values over old plot
hold on
followed by a new
plot(X, Y)
command
Add plot title
title(­'<T­itl­e>')
Add plot legend
legend­('<Name for plotted data>')
 
For Multiple plots: Separate names with commas.
Add x-axis label
xlabel­('<­Lab­el>')
Add y-axis label
ylabel­('<­Lab­el>')
Rotate y-axis label to be horizontal
ylabel­('<­Lab­el>', 'Rotat­ion', 0)
Turn on grid
grid on
Add text to plot
text(<X positi­on>, <Y positi­on>, '<T­ext­>')