6
我有3个txt文件s1.txt, s2.txt, s3.txt
。每个文件都有相同的格式和数量的数据。我只想将每个文件的第二列合并到一个文件中。如何将数据保存在MATLAB中的.txt文件中
未分类文件:之前我结合数据,我如第一列排序它 s1.txt s2.txt s3.txt
1 23 2 33 3 22
4 32 4 32 2 11
5 22 1 10 5 28
2 55 8 11 7 11
排序文件: s1.txt s2.txt S3 .TXT
1 23 1 10 2 11
2 55 2 33 3 22
4 32 4 32 5 28
5 22 8 11 7 11
这里是我的代码至今:
BaseFile ='s'
n=3
fid=fopen('RT.txt','w');
for i=1:n
%Open each file consecutively
d(i)=fopen([BaseFile num2str(i)'.txt']);
%read data from file
A=textscan(d(i),'%f%f')
a=A{1}
b=A{2}
ab=[a,b];
%sort the data according to the 1st column
B=sortrows(ab,1);
%delete the 1st column after being sorted
B(:,1)=[]
%write to a new file
fprintf(fid,'%d\n',B');
%close (d(i));
end
fclose(fid);
如何以这种格式获取新txt文件中的输出?
23 10 11
55 33 22
32 32 28
22 11 11
而不是这种格式?
23
55
32
22
10
33
32
11
11
22
28
11
。非常感谢您......您的代码非常整齐,它的工作原理! :)你刚刚度过我的一天!谢谢.. – Jessy 2010-05-28 01:03:57
@Jessy:代码可以做得更好。我没有太注意输入部分。例如,您实际上不需要cell2mat,只需在textscan中使用'CollectOutput'参数(true)即可。我还会添加验证代码以确保所有输入文件具有相同数量的行(或代码将不起作用)。 – yuk 2010-05-28 01:49:37
@Jessy,当我访问MATLAB时,我更新了代码。 – yuk 2010-05-28 15:31:12