如何在MATLAB中的.txt文件中保存数据 [英] How to save data in .txt file in MATLAB

查看:737
本文介绍了如何在MATLAB中的.txt文件中保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个txt文件 s1.txt,s2.txt,s3.txt 。每个文件都有相同的数据格式和数量。我只想组合第二个3个文件中每一个文件的列。在结合数据之前,我根据第一列进行了排序:

未排序文件:
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

以下是我到目前为止的代码: / p>

  BaseFile ='s'
n = 3
fid = fopen('RT.txt','w );
for i = 1:n
%连续打开每个文件
d(i)= fopen([BaseFile num2str(i)'。txt']);

从文件
读取数据A = textscan(d(i),'%f%f')
a = A {1}
b = A {2 }
ab = [a,b];

%根据第一列对数据进行排序
B = sortrows(ab,1);

删除排序后的第一列
B(:,1)= []

%写入新文件
fprintf(fid , '%d\\\
',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


解决方案

首先创建输出矩阵,然后将其写入文件。



以下是新代码:

  BaseFile ='s'; 
n = 3;
对于i = 1:n%不建议使用i或j作为变量,因为它们用于复杂的数学运算,但是我将把它留给你

%打开每个文件连续
d = fopen([BaseFile num2str(i)'.txt']);

%从文件
读取数据A = textscan(d,'%f%f','CollectOutput',1);

%根据第一列对数据进行排序
B = sortrows(A {:},1);

%而不是删除一列创建新矩阵
if(i == 1)
C = zeros(size(B,1),n);
end

检查输入文件并保存第二列
如果size(B,1)〜= size(C,1)
error('Input files有不同数量的行');
end
C(:,i)= B(:,2);

%不写
fclose(d);



写入新文件
fid = fopen('RT.txt','w'); (c,1)
fprintf(fid,[repmat('%d'\\t',1,n-1)'%d \ n'],C (K,:));
end
fclose(fid);

编辑:
其实只写数字到文件你不需要FPRINTF。使用 DLMWRITE 代替:

  dlmwrite('RT.txt',C,'\ t')


I have 3 txt files s1.txt, s2.txt, s3.txt.Each have the same format and number of data.I want to combine only the second column of each of the 3 files into one file. Before I combine the data, I sorted it according to the 1st column:

UnSorted file: 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

Sorted file: 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

Here is the code I have so far:

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);

How can I get the output in the new txt file in this format?

23 10 11 
55 33 22
32 32 28
22 11 11

instead of this format?

23    
55    
32   
22
10    
33
32
11
11
22
28
11

解决方案

Create the output matrix first, then write it to the file.

Here is the new code:

BaseFile ='s';
n=3;
for i=1:n % it's not recommended to use i or j as variables, since they used in complex math, but I'll leave it up to you

    % Open each file consecutively
    d=fopen([BaseFile num2str(i) '.txt']);

    % read data from file
    A=textscan(d,'%f%f', 'CollectOutput',1);

    % sort the data according to the 1st column
    B=sortrows(A{:},1);

    % Instead of deleting a column create new matrix
    if(i==1)
        C = zeros(size(B,1),n);
    end

    % Check input file and save the 2nd column
    if size(B,1) ~= size(C,1)
        error('Input files have different number of rows');
    end
    C(:,i) = B(:,2);

    % don't write yet
    fclose (d);

end

% write to a new file
fid=fopen('RT.txt','w');
for k=1:size(C,1)
    fprintf(fid, [repmat('%d\t',1,n-1) '%d\n'], C(k,:));
end
fclose(fid);

EDIT: Actually to write only numbers to a file you don't need FPRINTF. Use DLMWRITE instead:

dlmwrite('RT.txt',C,'\t')

这篇关于如何在MATLAB中的.txt文件中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆