matlab读取txt
matlab中讀取txt數(shù)據(jù)文件
根據(jù)txt文檔不同種類(lèi)介紹不同的讀取數(shù)據(jù)方法
一、純數(shù)據(jù)文件(沒(méi)有字母和中文,純數(shù)字)
對(duì)于這種txt文檔,從matalb中讀取就簡(jiǎn)單多了
例如test.txt文件,內(nèi)容為“17.901 -1.1111 33.045
????????????????????????????????????????? 17.891 -1.1286 33.045
????????????????????????????????????????? 17.884 -1.1345 33.045”
可以在command window中輸入load test.txt ,然后就會(huì)產(chǎn)生一個(gè)test的數(shù)據(jù)文件,內(nèi)容跟test.txt中的數(shù)據(jù)一樣;另一種方法是在file/import data....../next/finish 也可產(chǎn)生一個(gè)叫test的數(shù)據(jù)文件。
二、中英文和數(shù)據(jù)如test1.txt
“你好
歡迎來(lái)到
振動(dòng)論壇
vib.hit.edu.cn
1 11 111 1111
2 22 222 2222
3 33 333 3333
4 44 444 4444
5 55 555 5555”
這樣的文件怎么讀入數(shù)據(jù)呢?
方法有多種,現(xiàn)舉兩個(gè)比較簡(jiǎn)單實(shí)用的。
方法一:
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 =
??? '你好'
??? '歡迎來(lái)到'
??? '振動(dòng)論壇'
??? 'vib.hit.edu.cn'
方法二:
[a1,a2,a3,a4]=textread('test1.txt','%s%s%s%s','headerlines',4)
說(shuō)明:%s可以是其他形式,跟讀入的數(shù)據(jù)類(lèi)型有關(guān),比如這里也可以用%n,%f等。
這里%s的個(gè)數(shù)和[a1,a2,a3,a4]對(duì)應(yīng)。
>> [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'
因以字符串的形式讀入,所以有''。
————————————————————————————————
三、中文 數(shù)據(jù) 英文 混亂如test.txt
你好
1 11 111 1111
歡迎來(lái)到
2 22 222 2222
振動(dòng)論壇
3 33 333 3333
vib.hit.edu.cn
4 44 444 4444
5 55 555 5555
說(shuō)明:這種內(nèi)容格式的文件用上面的方法是不行的。
以下是由chinamaker編寫(xiě)的一種方法:
fidin=fopen('test.txt');?????????????????????????????? % 打開(kāi)test2.txt文件?????????????
fidout=fopen('mkmatlab.txt','w');?????????????????????? % 創(chuàng)建MKMATLAB.txt文件
while ~feof(fidin)????????????????????????????????????? % 判斷是否為文件末尾???????????????
??? tline=fgetl(fidin);???????????????????????????????? % 從文件讀行???
??? if double(tline(1))>=48&&double(tline(1))<=57?????? % 判斷首字符是否是數(shù)值
?????? fprintf(fidout,'%s\n\n',tline);????????????????? % 如果是數(shù)字行,把此行數(shù)據(jù)寫(xiě)入文件MKMATLAB.txt
?????? continue???????????????????????????????????????? % 如果是非數(shù)字繼續(xù)下一次循環(huán)
??? end
end
fclose(fidout);
MK=importdata('MKMATLAB.txt');????? % 將生成的MKMATLAB.txt文件導(dǎo)入工作空間,變量名為MK,實(shí)際上它不顯示出來(lái)?
>> MK
MK =
?????????? 1????????? 11???????? 111??????? 1111
?????????? 2????????? 22???????? 222??????? 2222
?????????? 3????????? 33???????? 333??????? 3333
?????????? 4????????? 44???????? 444??????? 4444
?????????? 5????????? 55???????? 555??????? 5555
總結(jié)
以上是生活随笔為你收集整理的matlab读取txt的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python 二维数组
- 下一篇: ssh 远程变更