matlab中各種數(shù)據(jù)的讀取_第1頁
matlab中各種數(shù)據(jù)的讀取_第2頁
matlab中各種數(shù)據(jù)的讀取_第3頁
matlab中各種數(shù)據(jù)的讀取_第4頁
matlab中各種數(shù)據(jù)的讀取_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、本技術(shù)支持指南主要處理:ASCII, binary, and MAT files.要得到MATLAB中可用來讀寫各種文件格式的完全函數(shù)列表,可以鍵入以下命令:help iofunMATLAB中有兩種文件I/O程序:high level and low level.High level routines: 包括現(xiàn)成的函數(shù),可以用來讀寫特殊格式的數(shù)據(jù),并且只需要少量的編程。Low level routines: 可以更加靈活的完成相對(duì)特殊的任務(wù),需要較多的額外編程。High level routines 包括現(xiàn)成的函數(shù),可以用來讀寫特殊格式的數(shù)據(jù),并且只需要少量的編程。

2、0;舉個(gè)例子,如果你有一個(gè)包含數(shù)值和字母的文本文件(text file)想導(dǎo)入MATLAB,你可以調(diào)用一些low level routines自己寫一個(gè)函數(shù),或者是簡單的用TEXTREAD函數(shù)。 使用high level routines的關(guān)鍵是:文件必須是相似的(homogeneous),換句話說,文件必須有一致的格式。下面的段落描述一些high level file I/O routines并給出一些例子幫助理解概念。 LOAD/SAVE 主要的high level file I/O routines 是LOAD 和 SAVE函數(shù)。LOAD

3、可以讀MAT-file data或者用空格間隔的格式相似的ASCII data. SAVE可以將MATLAB變量寫入MAT-file格式或者空格間隔的ASCII data。大多數(shù)情況下,語法相當(dāng)簡單。下面的例子用到數(shù)值由空格間隔的ASCII file sample_file.txt : 1 5 4 16 85 43 2 6 86 8 4 32 190 7 8 7 65 9 81 2 3 Example:用 LOAD and SAVE 讀寫數(shù)據(jù)CODE:% Load the file to the matrix, M :M = load('samp

4、le_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是基于圖形用

5、戶界面(GUI)的。會(huì)彈出對(duì)話框,列出當(dāng)前目錄的文件和目錄,提示你選擇一個(gè)文件。UIGETFILE讓你選擇一個(gè)文件來寫(類似Windows 另存為選項(xiàng)?)。用UIGETFILE,可以選擇已存在的文件改寫,也可以輸入新的文件名。兩個(gè)函數(shù)的返回值是所選文件名和路徑。 Example:用 UIGETFILE 從當(dāng)前目錄選擇一個(gè) M-fileCODE:% This command lists all the M-files in the current directory and% returns the name and path of the selected filefname,pna

6、me = uigetfile('*.m','Sample Dialog Box') 注意: UIGETFILE 一次只能選擇一個(gè)文件。 UIIMPORT/IMPORTDATA UIIMPORT是一個(gè)功能強(qiáng)大,易于使用的基于GUI的high level routine,用于讀complex data files。文件也必須是homogeneous。 IMPORTDATA形成UIIMPORT的功能,不打開GUI??梢詫MPORTDATA用于函數(shù)或者腳本中,因?yàn)樵诤瘮?shù)或者腳本中基于GUI的文件導(dǎo)入機(jī)制并不理想。下面的例子用到包

7、含幾行文件頭和文本、數(shù)值數(shù)據(jù)的文件'sample_file2.txt' : This is a file header.This is file is an example.col1 col2 col3 col4A    1   4    612.000B    1   4    613.000C    1   4    614.000D    1 

8、 4    615.000 Example: Using IMPORTDATA to read in a file with headers, text, and numeric dataCODE:% 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

9、the delimiter of the file, but IMPORTDATA can usually % detect this on its own D = importdata('sample_file2.txt','')  % 原文有誤?D = importdata('sample_file2.txt')可以通過訪問結(jié)構(gòu)D的數(shù)據(jù)和文本域,來看結(jié)構(gòu)D中的真實(shí)值,例如輸入:data = D.datatext = D.textdata 可以用UIIMPORT讀同一個(gè)文件并得到同樣的結(jié)構(gòu).&#

10、160;注意: 對(duì)于 ASCII data, 你必須檢驗(yàn)導(dǎo)入向?qū)д_的識(shí)別了列分隔符。 TEXTREAD/STRREAD TEXTREAD 是一個(gè)強(qiáng)大的動(dòng)態(tài)high level routine,設(shè)計(jì)用來讀ASCII格式的文本和/或數(shù)值數(shù)據(jù)文件。STRREAD除是從字符串而不是文件讀以外,類似于TEXTREAD。 兩個(gè)函數(shù)可以用許多參數(shù)來改變其具體的工作方式,他們返回讀入指定輸出的數(shù)據(jù)。他們有效的提供給你一個(gè)“兩全其美”的方法,因?yàn)樗麄兛梢杂靡粋€(gè)命令讀入混合的ASCII和數(shù)值數(shù)據(jù)(high level routines的做法),并且你可以改變他們以匹配你特

11、定的應(yīng)用(如同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:Ex

12、ample 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, wordswords = strread(file28,'%s','delimiter','')CODE:Example 3: Using TEXTREAD to read in text a

13、nd 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 outputsc1 c2 c3 c4 = textread('sample_file2.txt','%s %s %s %s','headerlines',2) CODE:Example 4: Using TEXTREAD to read in s

14、pecific 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',. 

15、9;%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 int

16、erest 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函數(shù)能夠讀寫分隔的ASCII data,而不是用low level routines。

17、他們比low level routines容易使用,Low level routines用幾行代碼實(shí)現(xiàn)的功能可以用DLMREAD/DLMWRITE簡化成一行。 CSVREAD用來讀分隔符是逗號(hào)的文件,是DLMREAD的特殊情況。當(dāng)讀空格和Tab分隔的電子數(shù)據(jù)表文件時(shí),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

18、 '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 th

19、e 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 c

20、reates 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指定范圍的指標(biāo)從0開始,而不是從1開始。 WK1READ/WK1WRITE WK

21、1READ 用來讀Lotus123 電子數(shù)據(jù)表文件的數(shù)據(jù);WK1WRITE用來寫矩陣到Lotus123 電子數(shù)據(jù)表文件。 XLSREAD XLSREAD用來讀Excel的數(shù)值和文本數(shù)據(jù)。  三. 具體例子分析:Matlab網(wǎng)站用兩個(gè)例子非常詳盡地介紹了各個(gè)命令的基本用法,實(shí)際中,面對(duì)手頭上的數(shù)據(jù),如何選用合適的命令呢?以下結(jié)合幾個(gè)示例給出一些總結(jié),大家舉一反三就可以了:1. 純數(shù)據(jù)(列數(shù)相同):源文件:CODE:0 3866.162 2198.938 141.1401 3741.139 2208.475 141.2522 3866.200 21

22、98.936 141.1563 3678.048 2199.191 141.2304 3685.453 2213.726 141.2615 3728.769 2212.433 141.2776 3738.785 2214.381 141.2567 3728.759 2214.261 141.2288 3748.886 2214.299 141.2439 3748.935 2212.417 141.25310 3733.612 2226.653 141.23611 3733.583 2229.248 141.22312 3729.229 2229.118 141.186解答:對(duì)于這個(gè)txt文件,

23、由于各行列數(shù)相同,故簡單地使用load,importdata均可。2.字段名(中、英文字段均可)+數(shù)據(jù):源文件:CODE:CH0 CH1 CH2 CH30.000123 0.000325 0.000378 0.0005980.000986 0.000256 0.000245 0.000698解答:由于是記錄的形式,因此各行列數(shù)必相同(缺少部分列時(shí)請(qǐng)自行在文件中補(bǔ)上 Inf 或 NaN),故直接使用 importdata 便可。3.注釋(含有獨(dú)立的數(shù)字串)+數(shù)據(jù)(列數(shù)相同):問題:這個(gè)文件有4列,但前6行是文字說明,4列數(shù)字是從第8行開始的.現(xiàn)在我想把這個(gè)文件的前2列和文字說明提出來組成一個(gè)新的

24、dat文件源文件:CODE:Group 2  12.02.2006   LimeiSamples of datas: 50000CH0  CH1  CH2  CH30.000123  0.000325   0.000378   0.0005980.000986  0.000256   0.000245   0.000698目標(biāo)文件:CODE:Group 2 12.02.2006

25、 LimeiSamples of datas: 50000CH0 CH10.000123 0.0003250.000986 0.000256解答:由于注釋中含有獨(dú)立的數(shù)字串,且注釋部分沒有明顯的格式, 這時(shí)候用importdata, load等高級(jí)命令直接讀取會(huì)失敗,用 textread, dlmwrite 等格式化命令也不太合適,因此只能使用低級(jí)命令進(jìn)行讀取。(當(dāng)然了,可以跳過注釋部分直接用高級(jí)命令讀取數(shù)據(jù),即:a b c d = textread(filename,'%f %f %f %f','headerlines',4); )。一個(gè)簡單的、非通用的包含注

26、釋的讀取方法如下:-轉(zhuǎn) -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  

27、%數(shù)值開始            a=strread(tline);            a(3:4)=;            fprintf(fid_n,'%f %fn',a);            clear a;    

28、   elseif double(tline(1)=67   %字母C開始           b1,b2,b3,b4=strread(tline,'%s %s %s %s');           b=b11,'  ',b21;            fp

29、rintf(fid_n,'%sn',b);            clear b b1 b2 b3 b4;        else            fprintf(fid_n,'%sn',tline);        end    else  

30、0;     fprintf(fid_n,'%sn',tline);    endendfclose(fid);fclose(fid_n);-4. 注釋(不含獨(dú)立的數(shù)字串)+數(shù)據(jù)(列數(shù)相同):源文件:CODE:你好 abc歡迎來到 我們振動(dòng)論壇1 11 111 11112 22 222 22223 33 333 33334 44 444 44445 55 555 5555解答:直接用 importdata 便可注:有時(shí)候注釋中含有獨(dú)立的數(shù)字串也可以 importdata 成功,不過得到的結(jié)果有可能不正確,建議這時(shí)候使用

31、第3種情形的讀取方式。5. 注釋與數(shù)據(jù)混排:對(duì)此當(dāng)然只能自己編程,舉例:源文件:CODE:1 11 111 1111你好2 22 222 2222歡迎來到3 33 333 3333振動(dòng)論壇4 44 444 44445 55 555 5555解答:-轉(zhuǎn)-CODE:function data=distilldata(infile)%功能說明:%將保存數(shù)據(jù)的原始文件中的數(shù)值數(shù)據(jù)讀入到一個(gè)data變量中%使用說明:% infile原始數(shù)據(jù)文件名;% data=數(shù)據(jù)變量tmpfile='tmp2.mat'fidin=fopen(infile,'r'); % 打開原始數(shù)據(jù)文

32、件(.list)fidtmp=fopen(tmpfile,'w'); % 創(chuàng)建保存數(shù)據(jù)文件(不含說明文字)while feof(fidin) % 判斷是否為文件末尾  tline=fgetl(fidin); % 從文件讀入一行文本(不含回車鍵)  if isempty(tline) % 判斷是否空行    m,n=size(tline);    flag=1;    for i=1:n %判斷一行中有沒有字符(.Ee和空格鍵除外)      

33、;if (tline(i)=' '|tline(i)='-'|tline(i)='.'|tline(i)='E'.          |tline(i)='e'|tline(i)='+'.          |(double(tline(i)>=48&&double(tline(i)<=57)       

34、; flag=0;        break;      end    end    if flag=1 % 如果是數(shù)字行,把此行數(shù)據(jù)寫入文件      fprintf(fidtmp,'%sn',tline);    end  endendfclose(fidin);fclose(fidtmp);data=textread(tmpfile);delet

35、e(tmpfile);-另外,如果要求不高,也可以使用 textread 函數(shù)跳過注釋部分進(jìn)行讀取,不過前提是需要事先知道文件內(nèi)容的結(jié)構(gòu)(即哪行是數(shù)據(jù)、哪行是注釋)6.各列數(shù)據(jù)的分離:源文件:CODE:           0 +  47038.7   1.05  09:26:07  C           2 +  46477.7

36、60;  1.03  09:28:38  C             4 +  44865.7   1.04  09:28:48  C             6 +  41786.4   1.03  09:2

37、8:56  C             8 +  39896.0   0.97  09:29:03  C            10 +  37518.4   0.93  09:29:15  C    

38、0;       12 +  35858.5   0.92  09:29:30  C            14 +  46105.0   1.03  09:30:21  C            16 +  46168

39、.6   6.89  09:30:30  C            18 +  48672.3   4.33  09:30:40  C            20 +  49565.7   0.49  09:30:48  

40、;C            22 +  49580.7   0.53  09:30:55  C            24 +  49602.3   0.84  09:31:03  C          

41、60; 26 +  49582.5   1.51  09:31:11  C            28 +  49577.0   1.39  09:31:19  C            30 +  49589.3   0.61 

42、 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,

43、f=textread(yourfilename,'%d %c %f %f %s %c'); 便可四. 注意事項(xiàng):1. 請(qǐng)?jiān)?matlab 中保持當(dāng)前路徑在該數(shù)據(jù)文件對(duì)應(yīng)的目錄下進(jìn)行存取,否則,存取時(shí)請(qǐng)給出該數(shù)據(jù)文件的具體路徑。 2. 存取時(shí),請(qǐng)給出該數(shù)據(jù)文件的全稱(包括后綴名,讀取mat文件時(shí)可省略) 3. load data.txt和A=load(data.txt)的區(qū)別請(qǐng)參閱精華貼: 4. 請(qǐng)根據(jù)讀寫需要來打開文件,即根據(jù)你的需要來指定 fopen 的 permission 屬性為讀或?qū)?。如果只?a 進(jìn)行寫入,就不能用 fread 讀取。此時(shí)

44、應(yīng)該寫完關(guān)閉文件,然后用 r 打開讀取,或者直接用 a+ 進(jìn)行同時(shí)讀寫操作。否則,會(huì)產(chǎn)生莫名其妙的問題!以下代碼是一個(gè)錯(cuò)誤的例子:CODE:filename='e.dat'fid=fopen(filename,'a');if fid<0    error('fopen error');ends=1 2 3 4;5 6 7 8;fwrite(fid,s,'float32')dd ll=fread(fid,inf,'float32');把t中的數(shù)據(jù)全部讀出,即s矩陣。fclose(fid);此

45、時(shí)得到的dd, ll 是錯(cuò)誤且無意義的!五. 其他相關(guān)問題:1. 連續(xù)讀取多個(gè)文件的數(shù)據(jù),并存放在一個(gè)矩陣中:(1) 首先是如何讀取文件名:方法一:filename=dir(*.jpg);那么第i個(gè)文件的文件名就可以表示為filename(i).name文件數(shù)量為:length(filename)方法二:先在Windows的 MSDOS(命令行)中使用以下命令生成一個(gè)list.txt文件:dir pathfolder /on /b /s > pathlist.txt 舉例:dir d:test /on /b /s > d:list.txt 然后在 matlab

46、中使用: filename = textread(sFileFullName,'%s'); 把所有文件名讀取到list細(xì)胞矩陣中,最后對(duì)filenamei便可得到各文件名。(2) 然后是讀取文件名的數(shù)據(jù)并存儲(chǔ):假設(shè)每個(gè)文件對(duì)應(yīng)的數(shù)據(jù)是m*n的,則:CODE:k = length(filename);Data = zeros(m,n,k);for ii = 1:k  Data(:,:,ii) = yourreadstyle(filenameii); %yourreadstyle是對(duì)應(yīng)的文件讀取方式的函數(shù)end2. 連續(xù)讀取多個(gè)文件的數(shù)據(jù),并

47、存放在多個(gè)矩陣(以文件名命名)中:假設(shè)每個(gè)文件對(duì)應(yīng)的數(shù)據(jù)是m*n的,則以上述第二種文件名讀取方法為例:CODE:k = length(filename);for ii = 1:k  D = yourreadstyle(filenameii);eval(Data_, num2str(ii), = D;);end3. 文件名命名問題:文件名為 abc00001,abc00002,. abc00009,abc00010,. abc00099,abc00100,.abc00879.  準(zhǔn)備把這些文件名給放到一個(gè)數(shù)組里面去。解答:CODE:a=cell(879,1);for k=1:879     ak = sprintf('%.5d',k);end4. 上述各種文件格式、類型自動(dòng)識(shí)別問題:可以利用正則表達(dá)式來處理,使之通用性較強(qiáng)。例如使用以下代碼可以自動(dòng)處理上面提到了例1到例5各種情形,不過由于存在自動(dòng)判斷,對(duì)某些例子(如例1)效率自然要低一點(diǎn),而對(duì)于另外的例子(如例3、例5)效率估計(jì)要高一點(diǎn)(少用了一個(gè)循環(huán))。COD

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論