2010-06-21 86 views
0

如何將兩個數據列組合成一個文件。這些代碼應該產生一個新的文件,其中有2列。雖然產生2列,但其中的a所有的數據被寫入第一後跟數據durationMATLAB組合數據

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

編輯的數據是不正確的: 被如下所示的輸出格式 -

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 
+0

你能告訴我們這兩列的格式嗎? – Jacob 2010-06-21 23:51:26

回答

0

你需要修復的fprintf行:

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

它不起作用.. – Jessy 2010-06-22 06:04:23

+0

現在好了嗎?如果不是的話,你能不能顯示'a'和'duration,並告訴我究竟出了什麼問題? – Jonas 2010-06-22 11:06:19