MATLAB文件I/O指南(3)高級文件I/O程序

 

2、高級文件I/O程序(High Level Routines)

High level routines包括現成的函數,可以用來讀寫特殊格式的數據,並且只需要少量的編程。

舉個例子,如果你有一個包含數值和字母的文本文件(text file)想導入MATLAB,你可以調用一些low level routines自己寫一個函數,或者是簡單的用TEXTREAD函數。

使用high level routines的關鍵是:文件必須是相似的(homogeneous,換句話說,文件必須有一致的格式。下面的段落描述一些high level file I/O routines並給出一些例子幫助理解概念。

LOAD/SAVE

主要的high level file I/O routines LOAD SAVE函數。LOAD 可以讀MAT-file data或者用空格間隔的格式相似的ASCII data. SAVE可以將MATLAB變量寫入MAT-file格式或者空格間隔的ASCII data。大多數情況下,語法相當簡單。下面的例子用到數值由空格間隔的ASCII file sample_file.txt

1 5 4 16 8

5 43 2 6 8

6 8 4 32 1

90 7 8 7 6

5 9 81 2 3

Example: 用 LOAD and SAVE 讀寫數據

% Load the file to the matrix, M :

 M = load('sample_file.txt') 

% Add 5 to M :

 M = M +5 

% Save M to a .mat file called 'sample_file_plus5.mat':

 save sample_file_plus5 M

% Save M to an ASCII .txt file called 'sample_file_plus5.txt' :

 save sample_file_plus5.txt M -ascii 

UIGETFILE/UIPUTFILE

UIGETFILE/UIPUTFILE是基於圖形用戶界面(GUI)的。會彈出對話框,列出當前目錄的文件和目錄,提示你選擇一個文件。UIGETFILE讓你選擇一個文件來寫(類似Windows ‘另存爲’選項?)。用UIGETFILE,可以選擇已存在的文件改寫,也可以輸入新的文件名。兩個函數的返回值是所選文件名路徑

Example: 用 UIGETFILE 從當前目錄選擇一個 M-file

% This command lists all the M-files in the current directory and

% returns the name and path of the selected file

[fname,pname] = uigetfile('*.m','Sample Dialog Box') 

注意: UIGETFILE 一次只能選擇一個文件。

UIIMPORT/IMPORTDATA

UIIMPORT是一個功能強大,易於使用的基於GUIhigh level routine,用於讀complex data files。文件也必須是homogeneous

IMPORTDATA形成UIIMPORT的功能,不打開GUI。可以將IMPORTDATA用於函數或者腳本中,因爲在函數或者腳本中基於GUI的文件導入機制並不理想。下面的例子用到包含幾行文件頭和文本、數值數據的文件 'sample_file2.txt'

This is a file header.

This is file is an example.

col1 col2 col3 col4

A    1   4    612.000

B    1   4    613.000

C    1   4    614.000

D    1   4    615.000

Example: Using IMPORTDATA to read in a file with headers, text, and numeric data

% This reads in the file 'sample_file2.txt' and creates a

% structure D that contains both data and text data.

% Note the IMPORTDATA command specifies a white space 

% as the delimiter of the file, but IMPORTDATA can usually 

% detect this on its own 

D = importdata('sample_file2.txt','')  % 原文有誤?

D = importdata('sample_file2.txt')

可以通過訪問結構D的數據和文本域,來看結構D中的真實值,例如輸入:

data = D.data

text = D.textdata 

可以用UIIMPORT讀同一個文件並得到同樣的結構.

注意: 對於 ASCII data, 你必須檢驗導入嚮導正確的識別了列分隔符。

 

    TEXTREAD/STRREAD

 

TEXTREAD 是一個強大的動態high level routine,設計用來讀ASCII格式的文本和/或數值數據文件。STRREAD除是從字符串而不是文件讀以外,類似於TEXTREAD

兩個函數可以用許多參數來改變其具體的工作方式,他們返回讀入指定輸出的數據。他們有效的提供給你一個 “兩全其美”的方法,因爲他們可以用一個命令讀入混合的ASCII和數值數據(high level routines的做法),並且你可以改變他們以匹配你特定的應用(如同low level routines做到的)。例子:

Example 1: Using TEXTREAD to read in an entire file into a cell array

% This command reads in the file fft.m into the cell array, file 

file = textread('fft.m','%s','delimiter','/n','whitespace',''); 

Example 2: Using STRREAD to read the words in a line

% This command uses the cell array created in Example 1 to 

% read in each word of line 28 in 'file' to a cell array, words

words = strread(file{28},'%s','delimiter','') 

Example 3: Using TEXTREAD to read in text and numeric data from a file with headers

% This command skips the 2 header lines at the top of the file

% and reads in each column to the 4 specified outputs

[c1 c2 c3 c4] = textread('sample_file2.txt','%s %s %s %s','headerlines',2) 

Example 4: Using TEXTREAD to read in specific rows of text and numeric data from a file

% This command reads in rows B and C of the file. The 'headerlines'

% property is used to move down to the desired starting row and the 

% read operation is performed 2 times 

[c1 c2 c3 c4] = textread('sample_file2.txt',... 

'%s %s %s %s',2,'headerlines',4) 

Example 5: Using TEXTREAD to read in only the numeric data from a file containing text and numbers

% This command reads in only the numeric data in the file. The

% 'headerlines' property is used to move down to the first row 

% of interest and the first column of text is ignored with the 

% '*'  operator 

[c2 c3 c4] = textread('sample_file2.txt','%*s %d %d %f','headerlines',3) 

DLMREAD/DLMWRITE/CSVREAD

DLMREAD DLMWRITE函數能夠讀寫分隔的ASCII data,而不是用low level routines。他們比low level routines容易使用,Low level routines用幾行代碼實現的功能可以用DLMREAD/DLMWRITE簡化成一行。

CSVREAD用來讀分隔符是逗號的文件,是DLMREAD的特殊情況。當讀空格和Tab分隔的電子數據表文件時,DLMREAD特別有用。以'sample_file.txt'爲例:

 Example 1: Using DLMREAD to read in a file with headers, text, and numeric data

% This reads in the file 'sample_file2.txt' and creates a matrix, D,

% with the numeric data this command specifies a white space as the

% delimiter of the file 

D = dlmread('sample_file.txt','') 

Example 2: Using DLMREAD to extract the first 3 columns of the last 3 rows

% This reads in the first 3 columns of the last 3 rows of

% the data file 'sample_file.txt'into the matrix, D_partial.

% 讀文件 'sample_file.txt' 3列後3行,到矩陣D_partial.

D_partial = dlmread('sample_file.txt','',[2 0 4 2]) 

Example 3: Using DLMWRITE to write a comma delimited file

% This creates a file called 'partialD.txt' that consists of 

% the first 3 columns of the last 3 rows of data where each

% element is separated by a comma 

dlmwrite('partialD.txt',D_partial,',') 

注意: 保證DLMREAD and DLMWRITE指定範圍的指標從0開始,而不是從1開始。

WK1READ/WK1WRITE

WK1READ 用來讀Lotus123 電子數據表文件的數據;WK1WRITE用來寫矩陣到Lotus123 電子數據表文件。

XLSREAD

XLSREAD用來讀Excel的數值和文本數據。

下面的解決方案提供High Level Routines的附加信息:

如何使文件頭中的名字s成爲載變量s的名字s
如何讀入ASCII分隔的有頭的數據文件?
如何讀逗號分隔的有數據缺失的ASCII 文?
如何使用LOAD and SAVE命令的the functional form?
如何導出a cell array containing double arrays ASCII文件格式?
當用導入嚮導導入二進制文件時,爲何MATLAB crash?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章