1 / 27

Outline

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

eliza
Download Presentation

Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Outline • 區域變數(Local Variables) • 全域變數(Global Variables) • 基本 I/O 操作 • 進階 I/O 操作 • 延伸 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 操作

  11. 進階 I/O 操作

  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 操作 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. 影像類別及型態

  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:可將資料寫成影像檔 • 最後一列敘述將會呼叫 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') • 對於不同的檔案格式,imfinfo 傳回的資訊項目可能有所不同。

More Related