matlab 读写text文档

来源:互联网 发布:php msgpack pack 编辑:程序博客网 时间:2024/05/16 12:34

一: matlab 读text文档

[a1,a2,a3,a4]=textread('C:\Desktop\test.txt','%s%s%d%d','headerlines',0)% %s表示为字符串,%d为整数,0代表从第几行开始读
test.txt 文档输入:

aaaabbbb33415ccccdddd34242132eeeeffff34341213sssswwww2343657ttttqqqq3435132aaaassss234434ccccerrrt556543

则a1, a2, a3, a4分别为8*1的数组,但a1, a2为cell类型。


二:matlab 写入text文档:将上述所得数组在写进text文档。(注意:因为a1,a2是cell类型不能直接写入文档,需做改变)

fid = fopen( 'C:\Desktop\test1.txt','w');for i=1:size(a1,1)    temp1 = a1(i);    temp2 = a2(i);    temp1 = cell2mat(temp1);    temp2 = cell2mat(temp2);    fprintf(fid,'%s\t%s\t%d\t%d\n',temp1,temp2,a3(i),a4(i));endfclose(fid);



原创粉丝点击