views:

142

answers:

1

How to combine two data columns into one file. These code should produce a new file which have 2 column. Although it produce 2 column but, the data isn't right where all the data of a were written first followed by data 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: The output format was as shown below -

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

The intended output is

24  1.1
24  1.08
24  2.1
24  0.77
1.3 1.8
A: 

You need to fix the fprintf line:

fprintf(fid,'%g\t%g\n',[a(:),duration(:)]');
Jonas
it doesn't work..
Jessy
better now? If not, can you show `a` and `duration, please, and tell me what exactly goes wrong?
Jonas
thanks Jonas :)
Jessy

related questions