【STM32F429的DSP教程】第4章 Matlab簡易使用之腳本文件
- 2020 年 4 月 3 日
- 筆記
完整版教程下載地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=94547
第4章 Matlab簡易使用之腳本文件
本期教程主要是講解Matlab的m文件簡易使用方法,有些內容跟上一節相同,但是比上一些更詳細。
4.1 初學者重要提示
4.2 Matlab的腳本文件.m的使用
4.3 Matlab中的條件和循環函數
4.4 繪圖功能
4.5 總結
4.1 初學者重要提
- 學習本章節前,務必優先學習第3章。
- 對於Matlab的m文件使用方法,務必要熟練掌握,後續章節都是基於m文件做測試。
4.2 Matlab的腳本文件.m的使用
在matlab上創建和使用.m文件跟在MDK或者IAR上面創建和使用.C或者.ASM文件是一樣的。創建方法如下:

點擊上圖中的小圖標,打開編輯窗口後,輸入以下函數:
r = rand(50,1); plot(r)
編輯好函數後需要將當前文件進行保存:

然後點擊如下圖標即可運行(或者按F5):

顯示效果如下:

4.3 Matlab中的條件和循環函數
matlab也支援類似C語言中的條件和循環語句:for, while, if, switch。但在matlab中使用比在C中使用更加隨意。
- 比如在.M文件中輸入以下函數:
nsamples = 5; npoints = 50; for k = 1 : nsamples currentData = rand(npoints,1); sampleMean(k) = mean(currentData); end overallMean = mean(sampleMean)
在命令窗口得到輸出結果:

- 為了將上面函數每次迭代的結果都進行輸出可以採用如下方法:
nsamples = 5; npoints = 50; for k = 1:nsamples iterationString = ['Iteration #',int2str(k)]; disp(iterationString) %注意這裡沒有分號,這樣才能保證會在命令窗口輸出結果 currentData = rand(npoints,1); sampleMean(k) = mean(currentData) %注意這裡沒有分號 end overallMean = mean(sampleMean) %注意這裡沒有分號
在命令窗口得到輸出結果:

- 如果在上面的函數下面加上如下語句:
if overallMean < .49 disp('Mean is less than expected') elseif overallMean > .51 disp('Mean is greater than expected') else disp('Mean is within the expected range') end
命令窗口輸出結果如下(這裡僅列出了最後三行):

4.4 繪圖功能
4.4.1 基本的plot函數
- 根據plot不同的輸入參數,主要有兩種方式:
- plot(y),這種方式的話,主要是根據y的數據個數產生一個線性曲線。
- plot(x,y)以x軸為坐標進行繪製。
比如在命令窗口或者.m文件中寫如下函數:
x = 0:pi/100:2*pi; y = sin(x); plot(x,y) xlabel('x = 0:2pi') ylabel('Sine of x') title('Plot of the Sine Function','FontSize',12)

- 下面這個函數可以實現在一個圖片上顯示多個曲線。
x = 0:pi/100:2*pi; y = sin(x); y2 = sin(x-.25); y3 = sin(x-.5); plot(x,y, x,y2, x,y3) legend('sin(x)','sin(x-.25)','sin(x-.5)')

- 另外曲線的樣式和顏色都可以進行配置的,命令格式如下:
plot(x, y, 'color_style_marker')

下面通過幾個實例看一下實際顯示效果。
x = 0:pi/100:2*pi; y = sin(x); plot(x,y,'ks')
顯示效果如下:

下面函數的顯示效果:
x = 0:pi/100:2*pi; y = sin(x); plot(x,y,'r:+')
下面函數的顯示效果如下:

- 複數繪圖
默認情況下函數plot只繪製數據的實部,如果是下面這種形式,實部和虛部都會進行繪製。plot(Z)也就是plot(real(Z),imag(Z))。下面我們在命令窗口中實現如下函數功能:
t = 0:pi/10:2*pi; plot(exp(i*t),'-o') axis equal
顯示效果如下:

- 在當前的繪圖中添加新的plot函數
使用函數hold on即可實現,這個函數我們在上一章節中已經使用過,作用就是在當前繪圖的基礎上加上一個新的繪圖。
% Obtain data from evaluating peaks function [x,y,z] = peaks; % Create pseudocolor plot pcolor(x,y,z) % Remove edge lines a smooth colors shading interp % Hold the current graph hold on % Add the contour graph to the pcolor graph contour(x,y,z,20,'k') % Return to default hold off
顯示效果如下:

- Axis設置
- 可見性設置
axis on %設置可見 axis off %設置不可見
-
- 網格線設置
grid on %設置可見 grid off %設置不可見
-
- 長寬比設置
axis square %設置X,Y軸等長 axis equal %設置X,Y相同的遞增。 axis auto normal %設置自動模式。
-
- 設置軸界限
axis([xmin xmax ymin ymax]) %二維 axis([xmin xmax ymin ymax zmin zmax]) %三維 axis auto %設置自動
4.4.2 繪製影像數據
下面通過一個簡單的實例說明一下影像數據的繪製,在命令窗口下操作即可。
>> load durer >> whos Name Size Bytes Class Attributes X 648x509 2638656 double ans 648x509 2638656 double caption 2x28 112 char map 128x3 3072 double >> image(X) %顯示圖片

>>colormap(map) %上色

>> axis image %設置坐標

使用相同的方法,大家可以載入圖片detail進行操作。另外用戶可以使用函數imwrite和imread操作標準的JPEG,BMP,TIFF等類型的圖片。
4.5 總結
本期主要跟大家講解了Matlab的簡單使用方法,需要大家多查手冊,多練習。