Presentation is loading. Please wait.

Presentation is loading. Please wait.

Outline 區域變數(Local Variables) 全域變數(Global Variables) 基本 I/O 操作

Similar presentations


Presentation on theme: "Outline 區域變數(Local Variables) 全域變數(Global Variables) 基本 I/O 操作"— Presentation transcript:

1 Outline 區域變數(Local Variables) 全域變數(Global Variables) 基本 I/O 操作

2 區域變數 區域變數(Local Variables) 每一個函數在運算時,均佔用個別的記憶體
此工作空間和 MATLAB 的基本工作空間或是其他函數的工作空間是互相獨立的 不同空間的變數是完全獨立,不會相互影響 不同工作空間的變數,稱為「區域變數」(Local Variables)

3 全域變數 減少變數的傳遞,可用「全域變數」(Global Variables) 使用全域變數前,需先進行變數宣告
function fun_eval global X % 全域變數宣告 X = X + 2; disp(['The value of X in "fun_eval" is ‘,num2str(X)]);

4 全域變數 fun_eval.m 沒有輸出和輸入,只宣告全域變數 X,將 X 的值加 2,並印出其值 測試
global X % 在基本工作空間進行全域變數 x 的宣告 X = 3; disp(['The value of X in main program is ‘,num2str(X)]); fun_eval; disp(['The value of X in main program is ‘,num2str(X),’ after fun_eval.’]);

5 全域變數 盡量少用全域變數 使用全域變數,遵循下列兩原則 全域變數使程式的流程不透明,造成程式除錯或維護的困難 使用前一定要宣告
建議使用全部大寫或較長的變數名稱,以便區別

6 基本 I/O 操作 有些計算曠日廢時,那麼通常希望能將長時間計算後所得的數據儲存起來,以方便將來進行其他後續的處理。MatLab 儲存變數的基本命令是 save,在不加任何選項(options)時,save 會將變數以二進制格式(binary)儲存至副檔名為 mat 的檔案。 # input, disp

7 基本 I/O 操作 save : 將 工作空間 的所有變數儲存到 matlab.mat 的二進制檔案。
save filename : 將 工作空間 的所有變 數儲存到以 filename.mat 為名的 二進制檔案。 save filename x y z : 將變數 x、y、z 儲 存到以 filename.mat 為名的二進 制檔案。 # who, whos, dir, ls, delete, !del, type

8 基本 I/O 操作 以 二進制 的方式儲存變數,檔案是最小的,且在 載入時 速度較快;但是無法用普通的文書軟體(如:記事本)看到檔案內容。若想看到檔案內容,則必須加上 -ascii 選項。 save filename x -ascii save filename x -ascii –double # load

9 基本 I/O 操作:binary vs. ascii

10 進階 I/O 操作 類別 支援檔案格式 指令 一般資料 試算表 MAT - MATLAB workspace load, save
CSV - comma separated numbers csvread, csvwrite DAT - formatted text Importdata DLM - delimited text dlmread, dlmwrite TAB - Tab separated text 試算表 XLS - Excel worksheet xlsread WK1 - Lotus 123 worksheet wk1read, wk1write

11 進階 I/O 操作 科學 資料 電影 影像 音訊 類別 支援檔案格式 指令
CDF - Common Data FormatFITS - Flexible cdfread, cdfwrite Image Transport System fitsread HDF - Hierarchical Data Format hdfread, hdfwrite 電影 AVI - Movie aviread 影像 TIFF, PNG, HDF, BMP, JPEG, GIF, PCX, XWD, CUR, ICO, RAS, PBM, PGM, PPM imread, imwrite 音訊 AU, SND - NeXT/Sun sound auread, auwrite WAV - Microsoft Wave sound wavread, wavwrite

12 延伸 I/O 操作 檔案控制 fopen:開啟檔案 fclose:關閉檔案 二進制資料 fread:用二進制格式從檔案讀取資料
fwrite:用二進制格式將資料寫入檔案

13 延伸 I/O 操作 具特定格式之資料 fscanf:讀取資料 fprintf:寫入檔案 fgetl:從檔案讀取一列資料, 捨去換行字元
fgets:從檔案讀取一列資料, 保留換行字元

14 延伸 I/O 操作 檔案位置控制 ferror:檔案輸入/輸出的錯誤狀態 feof:測試是否已到檔案結束位置 fseek:設定檔案定位器
ftell:讀取檔案定位器 frewind:回轉檔案定位器

15 延伸 I/O 操作 # dos x = 1:10; y = [x; sqrt(x)];
fid = fopen(‘squareRootTable.txt’, ‘w’); fprintf(fid,’Table of square root:\r\n’); fprintf(fid, ‘%2.0f => %10.6f\r\n’, y); fclose(fid); dos(‘start squareRootTable.txt’); % 開啟 squareRootTable.txt # dos

16 影像顯示與讀寫 影像顯示 影像讀取 影像寫入

17 影像顯示:索引影像(Indexed Images)
顯示此類型影像的語法如下: image(X) colormap(map) 其中 X 為影像的資料矩陣,map 為色盤矩陣。 色盤矩陣 map 的大小為 K×3,每個橫列由三個元素所組成,分別是 R(紅) 、G(綠)、B(藍) ,每個元素的範圍為 0~1 X 的為 m×n 矩陣,值介於 1~K 間,即當 X(i, j)的值為 p,則像素點 (i, j) 的顏色為 map(p, :) 決定。

18 影像顯示:索引影像(Indexed Images)
load clown.mat % 載入小丑影像資料 image(X); % 顯示影像 colormap(map) % 取用色盤矩陣 To check: min(min(X)), max(max(X)), size(map), size(X) X, map # load mandrill, axis image

19 影像顯示:索引影像(Indexed Images)
newmap = rand(size(map)); image(X); colormap(newmap)

20 影像讀取 imread imread:將影像檔之畫束資料讀出 X = imread('simulinkteam.jpg');

21 影像顯示:全彩影像(Truecolor Images)
全彩影像的資料為 m×n×3 矩陣,其中 X (:, :, 1) 代表 紅色的強度 X (:, :, 2) 代表 綠色的強度 X (:, :, 3) 代表 藍色的強度 矩陣的值之範圍可以是下列兩種: 介於 0~1 的 浮點數 或 0~255 的 uint8 image(X)

22 影像顯示:全彩影像(Truecolor Images)
To check: size(X), X, map [m, n] = size(X); figure ('unit', 'pixel', 'position', [10, 200, n, m]); image(X); set(gca, 'position', [0, 0, 1, 1]); set(gca, 'Visible', 'off'); set(gcf, 'PaperPositionMode', 'auto');

23 影像顯示:強度影像(Intensity Images)
如果色盤矩陣只有 K 個橫列,但是 X 的某些元素值小於 1 或大於 K,則將要使用imagesc 指令把 X 的最小值轉換成 1,最大值轉成 K,其他中間值則依線性關係轉換成介於 1 與 K 的值。 X = peaks; imagesc(X); colormap(gray); To check: min(min(X)), max(max(X))

24 影像類別及型態 資 料 型 態 影 像 類 別 雙精準(Double) uint8 索引影像 (Indexed Images)
資 料 型 態 影 像 類 別 雙精準(Double) uint8 索引影像 (Indexed Images) 影像矩陣大小:m×n 影像資料範圍:介於 [1, k] 的整數 影像資料範圍:介於 [0, k-1] 的整數 色盤矩陣大小:k×3 色盤資料範圍:介於 [0, 1] 的小數 影像顯示指令:image (註:k 的值不大於 256) 強度影像 (Intensity Images) 影像矩陣大小: m×n 影像資料範圍: 任意浮點 影像資料範圍:介於 [0, 255] 的整數 色盤資料範圍:介於 [0, 1] 的實數 影像顯示指令:imagesc (色盤通常是灰階) 全彩影像 (Truecolor Images) 影像矩陣大小: m×n×3 影像資料範圍:介於 [0,1] 的小數

25 影像類別及型態 雙精準的全彩影像轉作 uint8 資料型態: RGB8 = uint8(round(RGB64*255));
unit8 (8-bit)轉換成雙精準的全彩影像資料: RGB64 = double(RGB8)/255; 8-bit 影像轉回雙精準影像: Z64 = double(Z8)+1; uint8 資料型態亦可用於全彩影像資料,此時每一像素的原色(R,G 或 B)範圍為 0 至 255 間的整數,而不再是 0 至 1 的小數。

26 影像寫入 imwrite imwrite:可將資料寫成影像檔 load clown.mat
最後一列敘述將會呼叫 Windows 作業系統下的應用程式來開啟 myClown.jpg 檔案。同 dos('start myClown.jpg') load clown.mat imwrite( X, map, 'myClown.jpg'); !start myClown.jpg

27 影像資訊 imfinfo imfinfo:可用於傳回影像檔案的各項資訊 info = imfinfo('simulinkteam.jpg')


Download ppt "Outline 區域變數(Local Variables) 全域變數(Global Variables) 基本 I/O 操作"

Similar presentations


Ads by Google