Catalog / MATLAB Cheat Sheet

MATLAB Cheat Sheet

A concise reference for MATLAB syntax, commands, and functionalities, ideal for students, engineers, and researchers.

Basics & Syntax

Basic Commands

clc

Clears the command window.

clear

Removes all variables from the workspace.

clear x y

Removes variables x and y from the workspace.

who

Lists current variables in the workspace.

whos

Lists variables and their sizes, bytes, and class.

help command

Displays help for the specified command.

doc command

Opens the documentation page for the specified command.

exit or quit

Closes MATLAB.

Data Types

double

Default numeric type (double-precision floating point).

single

Single-precision floating point.

int8, int16, int32, int64

Signed integer types.

uint8, uint16, uint32, uint64

Unsigned integer types.

char

Character array (string).

logical

Boolean type (true or false).

cell

Cell array (can hold different data types).

struct

Structure array (fields with different data types).

Operators

=

Assignment operator.

+, -, *, /, ^

Arithmetic operators (addition, subtraction, multiplication, division, exponentiation).

.+, .-, .*, ./, .^

Element-wise arithmetic operators.

==, ~=, >, <, >=, <=

Relational operators (equal, not equal, greater than, less than, greater than or equal, less than or equal).

&&, ||, ~

Logical operators (AND, OR, NOT).

:

Colon operator (creates sequences).

Arrays and Matrices

Array Creation

[1 2 3]

Creates a row vector.

[1; 2; 3]

Creates a column vector.

[1 2; 3 4]

Creates a 2x2 matrix.

1:5

Creates a row vector from 1 to 5 (step size 1).

1:0.5:5

Creates a row vector from 1 to 5 (step size 0.5).

linspace(0, 10, 5)

Creates a row vector with 5 equally spaced points between 0 and 10.

zeros(2, 3)

Creates a 2x3 matrix of zeros.

ones(2, 3)

Creates a 2x3 matrix of ones.

eye(3)

Creates a 3x3 identity matrix.

Array Indexing

A(i, j)

Accesses the element in the i-th row and j-th column of matrix A.

A(i, :)

Accesses the i-th row of matrix A.

A(:, j)

Accesses the j-th column of matrix A.

A(1:3, 2:4)

Accesses a submatrix of A.

A(:)

Accesses all elements of A as a column vector.

Matrix Operations

A + B

Matrix addition.

A - B

Matrix subtraction.

A * B

Matrix multiplication.

A .* B

Element-wise multiplication.

A / B

Matrix right division (A * inv(B)).

A \ B

Matrix left division (inv(A) * B).

A .^ B

Element-wise exponentiation.

A'

Transpose of matrix A.

inv(A)

Inverse of matrix A.

Control Flow & Functions

Conditional Statements

if condition
  % statements
elseif condition
  % statements
else
  % statements
end

Looping

for i = 1:10
  % statements
end
while condition
  % statements
end

Functions

function [output1, output2] = my_function(input1, input2)
  % statements
  output1 = ...;
  output2 = ...;
end

Anonymous functions:

f = @(x) x.^2;
result = f(5); % result is 25

Switch Statement

switch expression
  case value1
    % statements
  case value2
    % statements
  otherwise
    % statements
end

Plotting

Basic Plots

plot(x, y)

Creates a 2D line plot of y versus x.

scatter(x, y)

Creates a scatter plot of y versus x.

bar(x, y)

Creates a bar chart.

histogram(data)

Creates a histogram of the data.

pie(data)

Creates a pie chart.

Plot Customization

title('Plot Title')

Sets the title of the plot.

xlabel('X-axis Label')

Sets the label for the x-axis.

ylabel('Y-axis Label')

Sets the label for the y-axis.

legend('Data Series')

Adds a legend to the plot.

grid on

Turns the grid on.

axis([xmin xmax ymin ymax])

Sets the axis limits.

hold on

Keeps the current plot and axes properties so that subsequent plotting commands add to the existing plot.

hold off

Releases the current plot and axes.

3D Plotting

plot3(x, y, z)

Creates a 3D line plot.

surf(X, Y, Z)

Creates a surface plot.

mesh(X, Y, Z)

Creates a mesh plot.

contour(X, Y, Z)

Creates a contour plot.