Summary of MATLAB Onramp
Summary of MATLAB Onramp
Basic Syntax
Example Description
x = pi Create variables and assign values with the equal sign (=).
The left side (x) is the variable name, and the right side (pi) is its value.
Desktop Management
Function Example Description
save save data.ma Save your current workspace to a MAT-file.
t
load load data.ma Load the variables in a MAT-file to the workspace.
t
Array Types
Example Description
4 scalar
[3 5] row vector
[1;3] column vector
[3 4 5; 6 7 8] matrix
Evenly Spaced Vectors
Example Description
1:4 Create a vector from 1 to 4, spaced by 1, using the colon operator (:).
1:0.5:4 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
rand(2) Create a square matrix with 2 rows and 2 columns.
Example Description
zeros(2,3 Create a rectangular matrix with 2 rows and 3 columns of 0s.
)
ones(2,3) Create a rectangular matrix with 2 rows and 3 columns of 1s.
Array Indexing
Example Description
A(end,2) Access the element in the second column of the last row.
Array Operations
Example Description
[1 2; 3 4] + 1 Perform array addition.
ans =
2 3
4 5
Multiple Outputs
Example Description
[xrow,xcol] = size(x Save the number of rows and columns in x to two different variables.
)
[xMax,idx] = max(x) Calculate the maximum value of x and its corresponding index value.
Documentation
Example Description
Plots
Example Description
plot(x,y,"ro--","LineWidth",5 Plot a red (r) dashed (--) line with a
) circle (o) marker, with a heavy line width.
Tables
Example Description
data.HeightYards Extract the variable HeightYards from
the table data.
data.HeightMeters = data.HeightYards*0.914 Derive a table variable from existing data.
4
Logical Indexing
Example Description
[5 10 15] > 12 Compare the elements of a vector to the value 12.
Programming
Example Description
if x > 0.5 If x is greater than 0.5, set y to 3.
y = 3
else Otherwise, set y to 4.
y = 4
end