Table of Contents
Basic knowledge
1. Load or save only some of your variables
The file myData.mat contains multiple variables. It was previously created for you to try loading just the variable m:
>> load myData m
Then try saving the variable m to a new MAT-file called justm.mat:
>> save justm m
2. Built in constants
>> a = pi
a =
3.1416
3. Built in functions
eg. abs (absolute value) and eig (calculate eigenvalues).
>> z = sqrt(-9)
z = 0.0000 + 3.0000i
Only the first four decimal places are displayed in the Command Window. You can control the displayed precision with the format function.
>> x = pi / 2
x =
1.5708
>> format long
>> x
x =
1.570796326794897
>> format short
>> x
x =
1.5708
format long : to display more digits
format short: to switch back to the default display.
Desktop MatLab
1. Create Live Script
How to select the command you just entered?
- Press Ctrl/Command + ↑ in the command window
- Press Shift + ↑ to select all the command you want to copy into Live Script
- Or press Ctrl/Command and select one by one
How to add textual comment?
- Press the LIVE EDITOR -> Text
Then press run to execute all commands in order
Array
1. linspace(first,last,num)
If you know the number of elements you want in a vector (instead of the spacing between each element), you could instead use the linspace function:
x = linspace(1,10,5)
2. Row array -> Column array
You can convert a row vector into a column vector using the transpose operator (’).
x = 1:3;
x = x'
x =
1
2
3
If you only use one index with a matrix, it will traverse down each column in order.
3. Get non-consecutive elements
Indices can be non-consecutive numbers. Try extracting the first, third, and sixth elements of density
.
p = density([1 2 6])
4. Matrix multiplication
The * operator performs matrix multiplication.
So, if you use * to multiply two equally sized vectors, since the inner dimensions do not agree, you will get an error message.
z = [3 4] * [10 20]
Error using *
Incorrect dimensions for matrix multiplication.
In contrast, the .* operator performs elementwise multiplication and allows you to multiply the corresponding elements of two equally sized arrays.
z = [3 4] .* [10 20]
z =
30 80
To learn more: Compatible Array Sizes for Basic Operations
5. Plotting Vectors
You can learn more about plot() in the documentation for Line Specification.
You can learn more about available properties in the documentation for Line Properties.
-
Notice that each plot command created a separate plot. To plot one line on top of another, use the
hold on
command to hold the previous plot while you add another line. -
Use
hold off
to return to the default plot behavior, where each plot gets its own axes.
There are many other plotting functions in MATLAB. You can see an extensive list in the MATLAB Plot Gallery.
6. fft()
A Fourier transform will return information about the frequency content of the signal. The location of the dominant frequencies will show what notes are contained in the chord.
You can use the fft
function to compute the discrete Fourier transform of a vector.
fft(y)
The output values from fft
are complex numbers. You can use the abs
function to get the magnitude.
These notes comes from MATLAB Onramp course