My Notes on how to use MatLab

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值