【Matlab】 讀取檔案各種方法
本技術支援指南主要處理:ASCII, binary, and MAT files.
要得到MATLAB中可用來讀寫各種檔案格式的完全函數清單,可以鍵入以下指令:
help iofun
MATLAB中有兩種檔案I/O程式:high level and low level.
High level routines: 包括現成的函數,可以用來讀寫特殊格式的資料,并且隻需要少量的程式設計。
Low 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 讀寫資料
CODE:
% 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
CODE:
% 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是一個功能強大,易于使用的基于GUI的high 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
CODE:
% 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做到的)。例子:
CODE:
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','');
CODE:
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','')
CODE:
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)
CODE:
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)
CODE:
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'為例:
CODE:
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','')
CODE:
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])
CODE:
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的數值和文本資料。
三. 具體例子分析:
Matlab網站用兩個例子非常詳盡地介紹了各個指令的基本用法,實際中,面對手頭上的資料,如何選用合适的指令呢?以下結合幾個示例給出一些總結,大家舉一反三就可以了:
1. 純資料(列數相同):
源檔案:
CODE:
0 3866.162 2198.938 141.140
1 3741.139 2208.475 141.252
2 3866.200 2198.936 141.156
3 3678.048 2199.191 141.230
4 3685.453 2213.726 141.261
5 3728.769 2212.433 141.277
6 3738.785 2214.381 141.256
7 3728.759 2214.261 141.228
8 3748.886 2214.299 141.243
9 3748.935 2212.417 141.253
10 3733.612 2226.653 141.236
11 3733.583 2229.248 141.223
12 3729.229 2229.118 141.186
解答:對于這個txt檔案,由于各行列數相同,故簡單地使用load,importdata均可。
2.字段名(中、英文字段均可)+資料:
源檔案:
CODE:
CH0 CH1 CH2 CH3
0.000123 0.000325 0.000378 0.000598
0.000986 0.000256 0.000245 0.000698
解答:由于是記錄的形式,是以各行列數必相同(缺少部分列時請自行在檔案中補上 Inf 或 NaN),故直接使用 importdata 便可。
3.注釋(含有獨立的數字串)+資料(列數相同):
問題:這個檔案有4列,但前6行是文字說明,4列數字是從第8行開始的.現在我想把這個檔案的前2列和文字說明提出來組成一個新的dat檔案
源檔案:
CODE:
Group 2 12.02.2006 Limei
Samples of datas: 50000
CH0 CH1 CH2 CH3
0.000123 0.000325 0.000378 0.000598
0.000986 0.000256 0.000245 0.000698
目标檔案:
CODE:
Group 2 12.02.2006 Limei
Samples of datas: 50000
CH0 CH1
0.000123 0.000325
0.000986 0.000256
解答:由于注釋中含有獨立的數字串,且注釋部分沒有明顯的格式, 這時候用importdata, load等進階指令直接讀取會失敗,用 textread, dlmwrite 等格式化指令也不太合适,是以隻能使用低級指令進行讀取。(當然了,可以跳過注釋部分直接用進階指令讀取資料,即:[a b c d] = textread(filename,'%f %f %f %f','headerlines',4); )。一個簡單的、非通用的包含注釋的讀取方法如下:
-------------------------------------轉 ---------------------------------------------------------------------------------------
CODE:
clc;clear;
fid = fopen('exp.txt', 'r');
fid_n=fopen('ex.dat','w');
while ~feof(fid)
tline=fgetl(fid);
if ~isempty(tline)
if double(tline(1))>=48 && double(tline(1))<=57 %數值開始
a=strread(tline);
a(3:4)=[];
fprintf(fid_n,'%f %f\n',a);
clear a;
elseif double(tline(1))==67 %字母C開始
[b1,b2,b3,b4]=strread(tline,'%s %s %s %s');
b=[b1{1},' ',b2{1}];
fprintf(fid_n,'%s\n',b);
clear b b1 b2 b3 b4;
else
fprintf(fid_n,'%s\n',tline);
end
else
fprintf(fid_n,'%s\n',tline);
end
end
fclose(fid);
fclose(fid_n);
---------------------------------------------------------------------------------
4. 注釋(不含獨立的數字串)+資料(列數相同):
源檔案:
CODE:
你好 abc
歡迎來到 我們
振動論壇
vib.hit.edu.cn
1 11 111 1111
2 22 222 2222
3 33 333 3333
4 44 444 4444
5 55 555 5555
解答:直接用 importdata 便可
注:有時候注釋中含有獨立的數字串也可以 importdata 成功,不過得到的結果有可能不正确,建議這時候使用第3種情形的讀取方式。
5. 注釋與資料混排:
對此當然隻能自己程式設計,舉例:
源檔案:
CODE:
1 11 111 1111
你好
2 22 222 2222
歡迎來到
3 33 333 3333
振動論壇
4 44 444 4444
vib.hit.edu.cn
5 55 555 5555
解答:
--------------------------------------------轉--------------------------------------
CODE:
function [data]=distilldata(infile)
%功能說明:
%将儲存資料的原始檔案中的數值資料讀入到一個data變量中
%使用說明:
% infile——原始資料檔案名;
% data=資料變量
tmpfile='tmp2.mat';
fidin=fopen(infile,'r'); % 打開原始資料檔案(.list)
fidtmp=fopen(tmpfile,'w'); % 建立儲存資料檔案(不含說明文字)
while ~feof(fidin) % 判斷是否為檔案末尾
tline=fgetl(fidin); % 從檔案讀入一行文本(不含Enter鍵)
if ~isempty(tline) % 判斷是否空行
[m,n]=size(tline);
flag=1;
for i=1:n %判斷一行中有沒有字元(+-.Ee和空格鍵除外)
if ~(tline(i)==' '|tline(i)=='-'|tline(i)=='.'|tline(i)=='E'...
|tline(i)=='e'|tline(i)=='+'...
|(double(tline(i))>=48&&double(tline(i))<=57))
flag=0;
break;
end
end
if flag==1 % 如果是數字行,把此行資料寫入檔案
fprintf(fidtmp,'%s\n',tline);
end
end
end
fclose(fidin);
fclose(fidtmp);
data=textread(tmpfile);
delete(tmpfile);
---------------------------------------------------------------------------------------------------------
另外,如果要求不高,也可以使用 textread 函數跳過注釋部分進行讀取,不過前提是需要事先知道檔案内容的結構(即哪行是資料、哪行是注釋)
6.各列資料的分離:
源檔案:
CODE:
0 + 47038.7 1.05 09:26:07 C
2 + 46477.7 1.03 09:28:38 C
4 + 44865.7 1.04 09:28:48 C
6 + 41786.4 1.03 09:28:56 C
8 + 39896.0 0.97 09:29:03 C
10 + 37518.4 0.93 09:29:15 C
12 + 35858.5 0.92 09:29:30 C
14 + 46105.0 1.03 09:30:21 C
16 + 46168.6 6.89 09:30:30 C
18 + 48672.3 4.33 09:30:40 C
20 + 49565.7 0.49 09:30:48 C
22 + 49580.7 0.53 09:30:55 C
24 + 49602.3 0.84 09:31:03 C
26 + 49582.5 1.51 09:31:11 C
28 + 49577.0 1.39 09:31:19 C
30 + 49589.3 0.61 09:31:27 C
32 + 49578.3 1.06 09:31:29 C
34 + 49512.5 1.77 09:31:38 C
解答:直接用 [a,b,c,d,e,f]=textread(yourfilename,'%d %c %f %f %s %c'); 便可
四. 注意事項:
1. 請在 matlab 中保持目前路徑在該資料檔案對應的目錄下進行存取,否則,存取時請給出該資料檔案的具體路徑。
2. 存取時,請給出該資料檔案的全稱(包括字尾名,讀取mat檔案時可省略)
3. load data.txt和A=load(‘data.txt’)的差別請參閱精華貼:
4. 請根據讀寫需要來打開檔案,即根據你的需要來指定 fopen 的 permission 屬性為讀或寫。如果隻用 a 進行寫入,就不能用 fread 讀取。此時應該寫完關閉檔案,然後用 r 打開讀取,或者直接用 a+ 進行同時讀寫操作。否則,會産生莫名其妙的問題!以下代碼是一個錯誤的例子:
CODE:
filename='e.dat';
fid=fopen(filename,'a');
if fid<0
error('fopen error');
end
s=[1 2 3 4;5 6 7 8];
fwrite(fid,s,'float32')
[dd ll]=fread(fid,inf,'float32');%把t中的資料全部讀出,即s矩陣。
fclose(fid);
此時得到的dd, ll 是錯誤且無意義的!
五. 其他相關問題:
1. 連續讀取多個檔案的資料,并存放在一個矩陣中:
(1) 首先是如何讀取檔案名:
方法一:
filename=dir(‘*.jpg’);
那麼第i個檔案的檔案名就可以表示為
filename(i).name
檔案數量為:length(filename)
方法二:
先在Windows的 MSDOS(指令行)中使用以下指令生成一個list.txt檔案:
dir path\folder /on /b /s > path\list.txt
舉例:dir d:\test /on /b /s > d:\list.txt
然後在 matlab 中使用:
filename = textread(sFileFullName,'%s');
把所有檔案名讀取到list細胞矩陣中,最後對filename{i}便可得到各檔案名。
(2) 然後是讀取檔案名的資料并存儲:
假設每個檔案對應的資料是m*n的,則:
CODE:
k = length(filename);
Data = zeros(m,n,k);
for ii = 1:k
Data(:,:,ii) = yourreadstyle(filename{ii}); %yourreadstyle是對應的檔案讀取方式的函數
end
2. 連續讀取多個檔案的資料,并存放在多個矩陣(以檔案名命名)中:
假設每個檔案對應的資料是m*n的,則以上述第二種檔案名讀取方法為例:
CODE:
k = length(filename);
for ii = 1:k
D = yourreadstyle(filename{ii});
eval([‘Data_’, num2str(ii), ‘ = D;’]);
end
3. 檔案名命名問題:
檔案名為 abc00001,abc00002,... abc00009,abc00010,... abc00099,abc00100,...abc00879. 準備把這些檔案名給放到一個數組裡面去。
解答:
CODE:
a=cell(879,1);
for k=1:879
a{k} = sprintf('%.5d',k);
end
4. 上述各種檔案格式、類型自動識别問題:可以利用正規表達式來處理,使之通用性較強。例如使用以下代碼可以自動處理上面提到了例1到例5各種情形,不過由于存在自動判斷,對某些例子(如例1)效率自然要低一點,而對于另外的例子(如例3、例5)效率估計要高一點(少用了一個循環)。
CODE:
function [data]=distilldata_eight(infile)
%功能說明:
%将儲存資料的原始檔案中的數值資料讀入到一個data變量中(自動判斷資料行)
%使用說明:
% infile——原始資料檔案名;
% data=資料變量
tmpfile='tmp2.mat';
fidin=fopen(infile,'r'); % 打開原始資料檔案(.list)
fidtmp=fopen(tmpfile,'w'); % 建立儲存資料檔案(不含說明文字)
while ~feof(fidin) % 判斷是否為檔案末尾
tline=fgetl(fidin); % 從檔案讀入一行文本(不含Enter鍵)
if ~isempty(tline) % 判斷是否空行
str = '[^0-9 | \. | \- | \s | e | E]'; %正規表達式為:該行中是否包含除 - . E e 數字 和 空白字元 外的其他字元
start = regexp(tline,str, 'once');
if isempty(start)
fprintf(fidtmp,'%s\n',tline);
end
end
end
fclose(fidin);
fclose(fidtmp);
data=textread(tmpfile);
delete(tmpfile)
5. 大量資料的讀取問題:
可以考慮使用循環分批讀取(特别是在各資料是獨立的時候),或者使用稀疏矩陣來實作。另外,也可參考《深入淺出MATLAB 7_X混合程式設計》一書第一章
6. 讀取整個txt檔案的内容(獲得檔案中的所有字元):
CODE:
f = fopen('yourfilename.txt','rt'); % t 屬性根據需要可省略
x = fread(f,'*char');
fclose(f);
7. 把維數不同的矩陣及其變量名儲存到一個 txt 檔案中,例如 a1 = 123; a2 = [1 2 3;4 5 6] ,希望得到的 txt 檔案如下:
QUOTE:
a1:
123
a2:
1 2 3
4 5 6
如果寫入的時候簡單一點,則可以采用以下方式,不過讀取的時候比較麻煩:
CODE:
a1=123;
a2=[1 2 3;4 5 6];
fid = fopen('myfile.txt', 'wt');
for i=1:2
fprintf(fid, '%s: \n %s\n', ['a',int2str(i)], mat2str(eval(['a',int2str(i)])));
end
fclose(fid);
相反,如果寫入的時候複雜一點,則讀取的時候會簡單一點:
CODE:
a1=123;
a2=[1 2 3;4 5 6];
fid = fopen('myfile.txt', 'wt');
for i=1:2
fprintf(fid, '%s: \n', ['a',int2str(i)]);
b = eval(['a',int2str(i)]);
fprintf(fid, [repmat('%d ', 1, size(b,2)), '\n'], b');
end
源自: http://www.cnblogs.com/xianghang123/archive/2011/12/06/2277602.html