matlab讀取文字檔

來源:互聯網
上載者:User

 

matlab 中如何讀取文字檔

今天需要做個matlab讀取txt檔案,在網上收集了下,查到了幾篇不錯的,總結一下,方便大家(包括me)使用:

下面這個函數是取filein中的第line行寫入fileout中的程式,如果想實現取特定幾行,只要稍微修改一下就可以。

function dataout=dataread(filein,fileout,line)

fidin=fopen(filein,'r');

fidout=fopen(fileout,'w');

nline=0;

while ~feof(fidin) % 判斷是否為檔案末尾

tline=fgetl(fidin); % 從檔案讀行

nline=nline+1;

if nline==line

fprintf(fidout,'%s\n',tline);

dataout=tline;

end

end

fclose(fidin);

fclose(fidout);

%%%%%%%%%%%%%%%%%%%%%%%%%%

調用格式:dataout=dataread(filein,fileout,line)

如果你的txt檔案資料是矩陣形式的,而沒有其它的文字,用下面的程式就可以讀任意行任意列的資料

a=textread('ll.txt');

t=a(1:43,4:10);

1:43是1到43行,4:10是4到10列的資料,當然也可以唯讀一個資料,如果你的matlab沒有textread函數,直接從mathworks網站下載就行。

根據txt文檔不同種類介紹不同的讀取資料方法

轉自:http://hi.baidu.com/youngbrave/blog/item/878db31fcd4f220f304e15bb.html

一、純資料檔案(沒有字母和中文,純數字)

對於這種txt文檔,從matalb中讀取就簡單多了

例如test.txt檔案,內容為“17.901 -1.1111 33.045

17.891 -1.1286 33.045

17.884 -1.1345 33.045”

可以在command window中輸入load test.txt ,然後就會產生一個test的資料檔案,內容跟test.txt中的資料一樣;另一種方法是在file/import data....../next/finish 也可產生一個叫test的資料檔案。

二、中英文和資料如test1.txt

“你好

歡迎來到

震動論壇

vib.hit.edu.cn

1 11 111 1111

2 22 222 2222

3 33 333 3333

4 44 444 4444

5 55 555 5555”

這樣的檔案怎麼讀入資料呢。

方法有多種,現舉兩個比較簡單實用的。

方法一:

file/import data....../next/finish

>> whos

Name Size Bytes Class

data 5x4 160 double array

textdata 4x1 300 cell array

Grand total is 54 elements using 460 bytes

>> data

data =

1 11 111 1111

2 22 222 2222

3 33 333 3333

4 44 444 4444

5 55 555 5555

>> textdata

textdata =

'你好'

'歡迎來到'

'震動論壇'

'vib.hit.edu.cn'

方法二:

[a1,a2,a3,a4]=textread('test1.txt','%s%s%s%s','headerlines',4)

說明:%s可以是其他形式,跟讀入的資料類型有關,比如這裡也可以用%n,%f等。

這裡%s的個數和[a1,a2,a3,a4]對應。

>> [a1,a2,a3,a4]=textread('test1.txt','%s%s%s%s','headerlines',4)

a1 =

'1'

'2'

'3'

'4'

'5'

a2 =

'11'

'22'

'33'

'44'

'55'

a3 =

'111'

'222'

'333'

'444'

'555'

a4 =

'1111'

'2222'

'3333'

'4444'

'5555'

因以字串的形式讀入,所以有''。

————————————————————————————————

三、中文資料 英文 混亂如test.txt

你好

1 11 111 1111

歡迎來到

2 22 222 2222

震動論壇

3 33 333 3333

vib.hit.edu.cn

4 44 444 4444

5 55 555 5555

說明:這種內容格式的檔案用上面的方法是不行的。

以下是由chinamaker編寫的一種方法:

fidin=fopen('test.txt'); % 開啟test2.txt檔案

fidout=fopen('mkmatlab.txt','w'); % 建立MKMATLAB.txt檔案

while ~feof(fidin) % 判斷是否為檔案末尾

tline=fgetl(fidin); % 從檔案讀行

if double(tline(1))>=48&&double(tline(1))<=57 % 判斷首字元是否是數值

fprintf(fidout,'%s\n\n',tline); % 如果是數字行,把此行資料寫入檔案MKMATLAB.txt

continue % 如果是非數字繼續下一次迴圈

end

end

fclose(fidout);

MK=importdata('MKMATLAB.txt'); % 將產生的MKMATLAB.txt檔案匯入工作空間,變數名為MK,實際上它不顯示出來

>> MK

MK =

1 11 111 1111

2 22 222 2222

3 33 333 3333

4 44 444 4444

5 55 555 5555

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.