1 / 21

檔案讀寫、圖形列印

檔案讀寫、圖形列印. 張智星 清大資工系 更新版:長庚大學機械系 吳俊仲. 高階的檔案讀寫指令. 常用讀寫檔案指令. save. 儲存計算結果至檔案。 用法: 將 X Y Z 三個變數存入 filename.mat save filename X Y Z 將 X Y Z 三個變數存入 filename.txt save filename.txt X Y Z -ascii. load. 由檔案讀出數值 用法 load filename (filename 不必為矩陣形式 ) load filename.txt – ascii

selma
Download Presentation

檔案讀寫、圖形列印

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. 檔案讀寫、圖形列印 張智星 清大資工系 更新版:長庚大學機械系 吳俊仲

  2. 高階的檔案讀寫指令 • 常用讀寫檔案指令

  3. save • 儲存計算結果至檔案。 • 用法: • 將 X Y Z 三個變數存入filename.mat • save filename X Y Z • 將 X Y Z 三個變數存入filename.txt • save filename.txt X Y Z -ascii

  4. load • 由檔案讀出數值 • 用法 • load filename • (filename 不必為矩陣形式) • load filename.txt –ascii • X=filename(1,:) • Y=filename(2,:) • Z=filename(3,:) • (filename.txt 必須為矩陣形式,非矩陣可用dlmread)

  5. save, load 例題: • x=1:10; • y=20:30; • z=1:50; • save test x y z; • clear • load test

  6. save, load 應用2 • x=1:10; • y=20:30; • z=1:50; • save test.txt x y z -ascii; • 不可用load

  7. csvread • 使用 csvread 指令來讀取 • 條件: • 資料檔案是由逗號分開 • 只有包含數值資料 • Ex:csvread01.m fprintf('data.csv 的內容:\n'); type data.csv % 列出 data.csv 的內容 A = csvread('data.csv') % 將 data.csv 的內容讀到矩陣 A

  8. result data.csv 的內容: 1, 2, 3 4, 5 6, 7, 8, 9 A = 1 2 3 0 4 5 0 0 6 7 8 9 • csvread 會傳回一個數值矩陣 • 其中缺席的資料將以 0 填入。

  9. dlmread • 如果數值資料的分界符號(Delimiters)不是逗點,就不能使用 csvread 指令,而要改用 dlmread 指令 • Ex:dlmread01.m fprintf('data.dlm 的內容:\n'); type data.dlm % 列出 data.dlm 的內容 A = dlmread('data.dlm', '\t') % 將 data.dlm 的內容讀到矩陣 A

  10. Result data.dlm 的內容: 1 2 3 4 5 6 7 8 9 A = 1 2 3 0 4 5 0 0 6 7 8 9 • 上例中data.dlm 的資料是以定位鍵(Tab)隔開,因此 dlmread 指令的第二個引數是 ‘\t’,以代表定位鍵

  11. textread • 如果檔案資料包含數值及字串,我們就必須改用 textread 指令 • Ex:textread01.m fprintf('data.txt 的內容:\n'); type data.txt % 列出 data.txt 的內容 [name, hobby, age] = textread('data.txt', '%s%s%d')

  12. result data.txt 的內容: Timmy OnlineGames 13 Annie Chatrooms 10 Roger Tennis 41 name = 'Timmy‘ 'Annie' 'Roger' hobby = 'OnlineGames' 'Chatrooms' 'Tennis' age = 13 10 41 • 在上述範例中,data.txt 包含三個欄位(或是三直行的資料) • textread 可在第二個引數指定資料型態 • 例如上例中 %s 代表字串,%d 代表整數 • 也同時將讀入的資料設定到不同的輸出引數 • 由於資料型態的不同,輸出引數也有不同的型態 • 以上述範例來說,name 和 hobby 都是字串異值陣列,而 age 則是數值陣列。

  13. 使用定位鍵來分隔欄位 • 上例中,我們利用空格來分隔欄位 • 如果欄位值本身也有空格 ? • 改用定位鍵來分隔欄位 • Ex:textread02.m fprintf('data2.txt 的內容:\n'); type data2.txt % 列出 data2.txt 的內容 [name, hobby, age] = textread('data2.txt', '%s%s%d', 'delimiter', '\t')

  14. result • 結果與前一例相同 • textread 指令中加上對分界字元(Delimiters)的定義,就可以讀出由定位鍵所分隔的資料檔案 • 若不指定時,預設為空白鍵

  15. 讀取文字檔 • textread 也可以讀取一個文字檔 • 同時把檔案內的每一列文字變成字串異質陣列裡面的每一個元素 • Ex:textread03.m Contents = textread('textread03.m','%s','delimiter','\n','whitespace',''); class(contents) % 印出 contents 的資料類別 contents{1} % 列出 contents 第一列 contents{2} % 列出 contents 第二列

  16. Result ans = cell ans = contents=textread('textread03.m','%s','delimiter','\n','whitespace',''); ans = class(contents) % 印出 contents 的資料類別 • 上例使用 textread 讀入 textread03.m(也就是此範例檔案),並顯示此檔案的第一列和第二列。 • textread 指令的用法還有很多,功能也很強大 • 在MATLAB下輸入「help textread」 • 可以得到完整的技術支援。

  17. 指令彙整 • 高階的文字檔案讀寫 • load/save • csvread/csvwrite • 讀寫以逗點分界欄位的數值資料檔案 • dlmread/dlmwrite • 讀寫以特定字元來當分界欄位的數值資料檔案 • textread • 讀入固定欄位的文字資料檔

  18. print • print (–device) –option filename • option可為 • djpeg: jpg圖檔 • dtiff: tiff圖檔 • dbitmap:bitmap圖檔 • dpng: png圖檔

  19. print • 例: • figure (1) • plot(x,y) • print –djpeg figure1 • (產生figure1.jpg) • figure(2) • plot(x,y) • print –dtiff figure2 • (產生figure2.tif)

  20. 例題 • x=0:0.01:pi; • y=sin(x); • save test.txt x y –ascii • dir • load test.txt –ascii • a1=test(1,:); • a2=test(2,:);

  21. 例題 (續) • plot(a1,a2) • print –djpeg figure1

More Related