1 / 31

檔案讀取與輸出儲存

檔案讀取與輸出儲存. 檔案載入. 載入存在的檔案 load S=load(‘ filename’) 將檔案的內容傳至變數 S 如果該檔案不在目前的資料夾,則必須寫出全路徑. s = 2.1000 243.0000 0.7040 2.1000 244.0000 0.7040 2.1000 245.0000 0.7920 2.1000 246.0000 0.7040 2.1000 247.0000 0.6160 2.1000 248.0000 0.5280

keely
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. 檔案載入 • 載入存在的檔案 load S=load(‘filename’) • 將檔案的內容傳至變數S • 如果該檔案不在目前的資料夾,則必須寫出全路徑 s = 2.1000 243.0000 0.7040 2.1000 244.0000 0.7040 2.1000 245.0000 0.7920 2.1000 246.0000 0.7040 2.1000 247.0000 0.6160 2.1000 248.0000 0.5280 2.1000 249.0000 0.5280 2.1000 250.0000 0.4400 2.1000 251.0000 0.4400 2.1000 252.0000 0.4400 Ex. >> S=load(‘c:\data1.txt’) >> S

  3. Fileread • 若只是要將一個檔案的內容載入成為一群字串, 可以使用 fileread 指令 out = fileread(‘filename’); 將out指定為filename的內容,但注意從此檔案中讀出的數值會變成字串而非矩陣,所以out的內容是一群字元

  4. Ex: 將data1.txt的內容讀出 >> out1=load(‘data1.txt’) %利用load >> out2=fileread(‘data1.txt’) %利用filereaed >> out1 ans = 2.1000 243.0000 0.7040 2.1000 244.0000 0.7040 2.1000 245.0000 0.7920 2.1000 246.0000 0.7040 2.1000 247.0000 0.6160 >> out2 ans = 2.1 243 0.704 2.1 244 0.704 2.1 245 0.792 2.1 246 0.704 2.1 247 0.616 out1和out2的不同點: out1為矩陣,可進一步取得運算所需的數值 out2為字串,得到的是一個個字元,而非可運算的數字

  5. 檔案儲存 • 儲存變數 save save filename X Y Z • filename為欲儲存的檔案名稱 • 將變數X, Y, Z存入此檔案中

  6. Ex: • >> load data1.txt • >> data1 • ans = • 2.1000 243.0000 0.7040 • 2.1000 244.0000 0.7040 • 2.1000 245.0000 0.7920 • 2.1000 246.0000 0.7040 • >> a1=data1(1,:)% 產生新的變數a1 • >> a1 • ans = • 2.1000 243.0000 0.7040 • >> save data3.mat a1 % 將變數a1存入data3.mat中

  7. 如果要再次使用data3.mat中的變數a1,可利用load >> clear all %先清除所有的變數 >> load data3.mat 鍵入whos可看到如下表所列,儲存在data3.mat中的 變數a1已經被載入了! >> whos Name Size Bytes Class a1 1x3 24 double array Grand total is 3 elements using 24 bytes • >> a1 • ans = • 2.1000 243.0000 0.7040

  8. 儲存影像 print print -device filename • -device 設定output file的格式 • filename欲儲存的圖檔名 Ex: print -dpng file.png 將圖檔file存成file.png檔

  9. 範例1(讀取檔案) >> close all; >> clear all; % 讀取data1.txt >> data=load(‘d:\data\data1.txt’); % 將不同欄的資訊取出令為變數,   利用這些變數來做資料處理 >> x=data(:,1); >> y=data(:,2); >> z=data(:,3);

  10. >> x(1:10)%列出x的前10項 ans = 2.1000 2.1000 2.1000 2.1000 2.1000 2.1000 2.1000 2.1000 2.1000 2.1000

  11. 範例2(儲存檔案) %接續上個範例,重新設定新的變數x1,y1,z1 >> x1=x(1:10); >> y1=y(1:10); >> z1=z(1:10); %將產生的新變數存入prodata.mat檔案中 >> save prodata x1 y1 z1;

  12. 檔案執行完畢,以後可利用load再將prodata.mat的資料取出檔案執行完畢,以後可利用load再將prodata.mat的資料取出 >> clear all %先清除所有的變數 >> load prodata 鍵入whos可看到有三個存入的變數 >> whos Name Size Bytes Class x1 10x1 80 double array y1 10x1 80 double array z1 10x1 80 double array Grand total is 30 elements using 240 bytes

  13. 開啟檔案 • 利用 fopen 函數來開啟檔案 fid = fopen(filename, permission) • 其中 filename 表示欲讀寫的檔案名稱 • permission 則表示欲對檔案進行的處理方式,預設值是 ‘r’ 讀取。它也可以是下列任一字串: • ‘r’:只准讀取(reading)檔案 • ‘w’:只准寫入(writing)檔案 • ‘a’:只准加入(appending)檔案 • 'a+':可讀取及加入檔案(reading and appending)

  14. 在windows下,permission 字串必需能夠分辨binary或 ASCII 檔案。 • 例如:若要讀binary檔案,則 permission 字串必需是“rb” • fopen 函數傳回一個檔案辨識碼,通常是個非負的整數,我們可用此辨識碼來對此檔案進行各種讀寫的處理。

  15. Ex1: [fid, message] = fopen(‘file1', 'r'); fprintf('fid = %d\n', fid); fprintf('message = %s\n', message); • 上例可知當檔案不存在時,回傳的 fid 是 –1 • 同時 message 會包含相關的錯誤資訊。 Output: fid = -1 message = Sorry. No help in figuring out the problem . . .

  16. 若開啟成功,則傳回的 fid 是一個大於 2 的整數,而且傳回的 message 是一個空字串。 Ex2: [fid, message] = fopen(‘file1', 'r'); fprintf('fid = %d\n', fid); fprintf('message = %s\n', message); Output: fid = 3 message =

  17. 關閉檔案 • 完成檔案的讀寫之後,可用 fclose 函數來關閉檔案 status = fclose(fid); • 若一切順利,fclose 傳回 0。 • 若無法順利關閉檔案,則 fclose 傳回 -1。 • 為避免因開啟檔案過多而造成系統資源浪費,一般在完成檔案的讀寫後,即應使用 fclose 來關閉檔案

  18. 若要一次關閉所有開啟的檔案,可用 fclose('all') 或是 fclose all。 • 開啟及關閉檔案都是比較耗時的函數,因此盡量不要將他們置於迴圈之中,以提高程式執行效率。

  19. 讀取ASCII 檔案 • fgetl 函數: • 可將 ASCII 檔案的內容中的某一列讀出 • 並將該列的 ASCII 內容以轉成字串傳回。 Ex1: fid = fopen(‘file1’, ‘r’); while feof(fid)==0 % feof 測試檔案指標是否已到達結束位置 line = fgetl(fid); disp(line); end

  20. 執行上述程式後,MATLAB 會 • 先在目前目錄找尋 file1 • 輸入「which file1」可顯示檔案所在的路徑 • fgets 和 fgetl 均可由檔案讀取一列資料: • fgetl 會捨去換行字元 • fgets 函數則保留換行字元。

  21. fscanf • 函數fscanf可對ascii檔案作更精確的讀取 matrix = fscanf(fid, format) • 其中 fid 是欲讀取之檔案的辨識碼 • format 是格式指定字串,常用的格式指定字串有下列幾種: • %s:字串 • %d:10進位的整數 • %g:雙倍精準(Double-precision)的浮點數(Floating-point Numbers)

  22. 注意fscanf在讀取檔案時,是一列一列進行的。但在傳回矩陣時,是將資料一行一行地填入欲傳回的矩陣中。注意fscanf在讀取檔案時,是一列一列進行的。但在傳回矩陣時,是將資料一行一行地填入欲傳回的矩陣中。 Ex1: 文字檔 test.txt 內容如下 1 4 9 16 25 36 49 64 81 100 欲使用 fscanf 指令讀取其內容,可輸入如下 fid = fopen('test.txt', 'r'); myData = fscanf(fid, '%g'); fclose(fid); myData% 顯示 myData

  23. myData = 1 4 9 16 25 36 49 64 81 100 • 此例顯示了 MATLAB 的 fscanf 指令和 C 的 fscanf 指令的最大不同: • MATLAB 的 fscanf 指令是向量化的 • 只要讀入資料的型態正確,MATLAB 的 fscanf 指令會一再執行 • 同時把所得結果存放於一個向量並回傳。

  24. sscanf • sscanf 函數和 fscanf 的功能很類似 • sscanf 函數從字串(Strings)中讀取資料 Ex: str = num2str([pi, sqrt(2), log10(3)]) % 建立一字串str retrieved = sscanf(str, ‘%g’) % 擷取str中的double Output: str = 3.1416 1.4142 0.47712 retrieved = 3.1416 1.4142 0.4771

  25. 寫入 ASCII 檔案 • fprintf 函數可將資料依格式指定字串來寫入 ASCII 檔案,其使用語法如下: fprintf(fid, format, y) • 其中 fid 是欲寫入之檔案的辨識碼 • format 是格式指定字串,用以指定資料寫至檔案的格式 • y 是 MATLAB 的資料變數,常用的格式指定字串有下列幾種: • %e:科學記號,即將數值表示成 a×10b 的形式 • %f:固定欄寬(含整數與小數部份)的表示法 • %g:自動選取 %e 或 %f

  26. fprintf在函式讀取變數時,是一直行一直行地讀,然後再一列一列地寫入檔案。fprintf在函式讀取變數時,是一直行一直行地讀,然後再一列一列地寫入檔案。 Ex:將平方根表寫入檔案 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); % 2.0f 印出的總欄寬為 2,且不帶小數 % 10.6f 印出的總欄寬為 10,包含 6位的小數 fclose(fid);

  27. sprintf • sprintf 函數和 fprintf 函數的功能很類似 • sprintf 將資料以字串形式傳回 Ex: >> str = sprintf('log(%f) = %e\n', 2, log(2)) Output: str = log(2.000000) = 6.931472e-001

  28. 讀取二進制資料 • 用fread 函數可從檔案中讀取二進制資料 • fread會將每一個位元組看成一個整數,並將結果以一矩陣傳回。 • 例如,檔案 test2.txt 的內容如下: This is a test!

  29. Ex1: fid = fopen('test2.txt', 'r'); myData = fread(fid); char(myData') % 驗證所讀入的資料是否正確 fclose(fid); Output: This is a test! • char 可將 myData 的整數轉成 ASCII 字元 • 取 myData 的轉制是為了使印出的效果易於閱讀

  30. fread 函數可用第二個輸入引數來控制傳回 矩陣的大小 Ex2: fid = fopen('test2.txt', 'r'); myData = fread(fid, 4) % 只讀 4 個位元組 fclose(fid); Output: myData = 84 104 105 115

  31. Ex3: fid = fopen('test2.txt', 'r'); myData = fread(fid, [2 3]) %此時 myData 為 2X3 的矩陣。 fclose(fid); myData = 84 105 32 104 115 105 Output:

More Related