Summary of MATLAB Onramp
Basic Syntax
Exemplo
ant (Create varlables and assign values with the equal sign (=).
‘The loft side (x) is the variable name, and the right side (pi J is its value.
y_= sin(-5) Provide inputs to a function using parentheses.
Desktop Management
Function Example Description
save | seve dete.nat | Save your current workspace lo a MAT-file
oad load data.nat Load the variablas in a MAT-fle tothe workspace
clear clear Clear ellvaviables fom the workspace
ac ale Claar alte from the Command Window.
format format long Change how numeric output appears in te Command Window.
Array Types
Example Descript
4 sealer
BS] rw vector
033] column vector
[3 45; 67 8] matric
Evenly Spaced Vectors
Example Description
aa Create a vector from 1 to 4, spaced by 1, using the colon operator (+Evenly Spaced Vectors
Example Deseription
aa Create a vector from 1 to 4, spaced by 1, using the colon operator
S24 Create a vector from 1 to 4, spaced by 0.5.
Linspace(1,10,5) Create a vector with 5 elements. The values are evenly spaced from 1 to
10
Matrix Creation
Example Description
and(2) Create a square matrix with 2 rows and 2 columns.
zeres(2,3) Create a rectangular mattix with 2 rows and 3 columns of as.
ones (2,3) Create a rectangular matrix with 2 raws and 3 columns of 15,
Array Indexing
Example Description
‘A(end,2) Access the element in the second column of the last rovr
(2,2) Access the entire second row.
A(L:3,+) Access all columns of the first three rows,
‘A(2) = 12, Change the value of the second element of an array to 12
Array Operations
Example Description
[22;34)42 erform array edi
203
@ 4s
[245 2 ayt[2 25 2 2] Perform matric multiplication‘Array Operations
Example Description
pores Porform array addltion.
ane]
45
[245 12] [2 25 2.2) _ Perform matix muttiptication
a4
al %
[1 a5 1 a)."[2 2; 2 2 Perform clementanise mutiplicaion
Multiple Outputs
Example Description
[xrow,xcol] = size(x) Save the number of rows and columns in x to two different variables,
[wlex, idx] = max(x) Calculate the maximum yelue of x and its corresponding index value.
Documentation
Example Description
doc randi Open the documentation page for the randi function,
Plots
Example Description
plot(x,y,"*o--","Lineliddth",S) Plata red (r) dashed (--) line with a
circle (0) marker, with a heavy line width
hold on Add the next line to the existina olat.Plots
Example Description
Linevicth",5) Plot red(r) dashed ( --)line with a
Girola () markor, with a heavy line width
plot (x.y,"ro-
hold on ‘Add the next line to the existing plot.
Create new axes for the next plotted line.
Add a title to a plot.
xdabel("x") Add labels to axes.
ylabel(“y")
Asgend("2","b", Add a legend to a plot.
Tables
Description
Extract the variable HeightYerds from the
table data
data.Heighteters = [email protected] Derive a table variable from existing data.
Logical Indexing
Example Description
[5.20.15] > 12 Compare the elements of a vector to the value 12.
vitvi> 6) Extract all elements of v2 that are greater than 6
x(x-=999) = 1 Replace all values in x thet are equal to 999 withthe value 2.
Programming
Example Description
if x > 0.5 xe greater than 0.5, eet y to 3
y=3
- Othanaica sat setaProgramming
Example Description
Lf x > 0.5 lf is greaterthan 0.5, set y to 3
ys3
disa Otherwise, sat y to 4
yess
end
for ¢ = 1:3. The loop counter (c) progresses through the
disp(c) Yalues 1:3 (2, 2, and 3)
end
The loop body displays each value of ¢