Matlab读取txt文件并找出开头相同的行并输出成另一个txt文件
我想用matlab读取txt文件,然后找出文件中开头相同的那些列并输出。比如txt文件如下:breaddiaperbeereggmilkmilkdiaperbeercoo...
我想用matlab读取txt文件,然后找出文件中开头相同的那些列并输出。
比如txt文件如下:
bread diaper beer egg milk
milk diaper beer cook
bread milk diaper beer
bread butter diaper milk
coffee sweat cookie fish
bread butter coffee diaper milk egg
bread butter fish chicken
egg bread butter
fish diaper milk
coffee sweat chicken egg
每一行的数据数量不一定,而且可能还行有数字。我希望能把开头都为”bread“的那些行都选择出来并输出到另一个txt文件里。
我写的是:
a=load('sample.txt');
firstNum=a(:,1);
b=find(firstNum==1);
c=a(b,:);
disp(c);
但load貌似不能输入个数不一样且英文数字混合的文件,请教大神如何修改,谢谢了~~!!
错了,不是输出列,是输出开头相同的”行“,谢谢!! 展开
比如txt文件如下:
bread diaper beer egg milk
milk diaper beer cook
bread milk diaper beer
bread butter diaper milk
coffee sweat cookie fish
bread butter coffee diaper milk egg
bread butter fish chicken
egg bread butter
fish diaper milk
coffee sweat chicken egg
每一行的数据数量不一定,而且可能还行有数字。我希望能把开头都为”bread“的那些行都选择出来并输出到另一个txt文件里。
我写的是:
a=load('sample.txt');
firstNum=a(:,1);
b=find(firstNum==1);
c=a(b,:);
disp(c);
但load貌似不能输入个数不一样且英文数字混合的文件,请教大神如何修改,谢谢了~~!!
错了,不是输出列,是输出开头相同的”行“,谢谢!! 展开
1个回答
展开全部
关于文本数据读取我做过一些研究, 下面是解决你的问题的一个方法.
---- sample.txt ---------------------------
bread diaper beer egg milk
milk diaper beer cook
bread milk diaper beer
bread butter diaper milk
coffee sweat cookie fish
bread butter coffee diaper milk egg
bread butter fish chicken
egg bread butter
fish diaper milk
coffee sweat chicken egg
--- m file code ---------------------------
clc;
file = 'sample.txt';
fid = fopen(file,'r');
disp(['Reading file: ',file]);
linenumber = 0;
while ~feof(fid)
linenumber = linenumber + 1;
line = fgetl(fid);
if(line(1:6) == 'bread ')
disp([num2str(linenumber),':',line]);
end
end
---- output -------------------------------
Reading file: sample.txt
1:bread diaper beer egg milk
3:bread milk diaper beer
4:bread butter diaper milk
6:bread butter coffee diaper milk egg
7:bread butter fish chicken
---- sample.txt ---------------------------
bread diaper beer egg milk
milk diaper beer cook
bread milk diaper beer
bread butter diaper milk
coffee sweat cookie fish
bread butter coffee diaper milk egg
bread butter fish chicken
egg bread butter
fish diaper milk
coffee sweat chicken egg
--- m file code ---------------------------
clc;
file = 'sample.txt';
fid = fopen(file,'r');
disp(['Reading file: ',file]);
linenumber = 0;
while ~feof(fid)
linenumber = linenumber + 1;
line = fgetl(fid);
if(line(1:6) == 'bread ')
disp([num2str(linenumber),':',line]);
end
end
---- output -------------------------------
Reading file: sample.txt
1:bread diaper beer egg milk
3:bread milk diaper beer
4:bread butter diaper milk
6:bread butter coffee diaper milk egg
7:bread butter fish chicken
更多追问追答
追问
哇,谢谢大神~~成功了,但请问如何将生成的新数组以txt文件导出呢?
我写的是:
fid1=fopen('E:\new.txt');
fprintf(fid1,[line]);
fclose(fid1);
但最后存进去的不对。。求指导~~谢谢!
追答
你只是缺少个换行符而已
fid1=fopen('E:\new.txt','w');
fprintf(fid1,[line,'\n']);
fclose(fid1);
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询