MATLAB结合数据

时间:2010-06-21 22:39:27

标签: matlab

如何将两个数据列合并为一个文件。这些代码应该生成一个有2列的新文件。虽然它产生了2列但是,数据不正确,a的所有数据首先写入,然后是数据duration

fid=fopen('data1.txt');
A  =textscan(fid,'%f%f%f%f%f%f%f%f%f%f%f%f'); % read a txt file
in = cell2mat(A); %change to matrix
fclose(fid);

index = find(in(2:end,2) == in(1:end-1,2)) + 1; %condition 1
duration(index)= in(index,4) - in(index-1,4); 
a(index)=in(index,2);

fid = fopen('test.txt','wt');
format short g;
fprintf(fid,'%g\t%g\n',a,duration);
fclose(fid);

EDITED: 输出格式如下所示 -

318684 24    % 318684 I don't know where this number come from, not from the input
24     24    % this is the a output
24     24
1.1    1.08  % this is the duration output
2.1    0.77

预期输出

24  1.1
24  1.08
24  2.1
24  0.77
1.3 1.8

1 个答案:

答案 0 :(得分:0)

您需要修正fprintf行:

fprintf(fid,'%g\t%g\n',[a(:),duration(:)]');