match up two files in matlab
I have multiple files with varying file lengths.
file 1 is a .txt file of length 86400 X 10 file 2 is a .csv file of length 144 X 10
Now this is the case when both the files have all data , but lot of times i have missing time stamps in middle 开发者_Python百科so the length of the file1 is 1000-86400 X 10 and the file 2 is 10-144 X 10
Now I want to write a third file of length 144 X 20 to include all the columns of file 2 and file 1( I am averaging the 86400 down to 144).
My problem is I am not able to match up the times properly to create the third file Any help?
file2
datetime, B1, B2, B3, B4, B5, B6 ......
9/15/2010 0:00, 8.8, 3.8, 2.8, 7.3, 0, 9.9
9/15/2010 0:10, 10.1, 8.4, 5.1, 7.7, 0, 8.1
9/15/2010 0:20, 8.8, 5.4, 7.6, 7.1, 0, 10.3
9/15/2010 0:30, 5.7, 8.8, 7.1, 1.6, 0, 6.8
9/15/2010 0:50, 7.9, 9.2, 6.4, 6.2, 0, 8.4
9/15/2010 1:20, 0.6, 5.85, 8.1, 9.8, 0, 0.6
file 1
datetime, A1, A2, A3, A4, A5, A6......
9/15/2010 0:00:01, 8.8, 3.8, 2.8, 7.3, 0, 9.9
9/15/2010 0:00:02, 10.1, 8.4, 5.1, 7.7, 0, 8.1
9/15/2010 0:00:03, 8.8, 5.4, 7.6, 7.1, 0, 10.3
9/15/2010 0:00:09, 5.7, 8.8, 7.1, 1.6, 0, 6.8
9/15/2010 0:00:10, 7.9, 9.2, 6.4, 6.2, 0, 8.4
9/15/2010 1:00:03, 0.6, 5.85, 8.1, 9.8, 0, 0.6
I use to read the files
fid = fopen('file1.csv', 'rt');
a = textscan(fid, '%s %f %f %f %f %f %f', ...
'Delimiter',',', 'CollectOutput',1, 'HeaderLines',1);
fclose(fid);
M = [datenum(a{1}) a{2}]
fid = fopen('file2.txt', 'rt');
b = textscan(fid, '%s %f %f %f %f %f %f', ...
'Delimiter',',', 'CollectOutput',1, 'HeaderLines',1);
fclose(fid);
N = [datenum(b{1}) b{2}]
How do i write a 3rd file with all the columns of file 1 and file2 with the matching time stamps in the same format as file 2 ??
I am trying to write a 3rd file as
datetime, B1, B2, B3, B4, B5, B6 ...... A1 A2 A3 A4 .......
9/15/2010 0:00, 8.8, 3.8, 2.8, 7.3, 0, 9.9 x
9/15/2010 0:10, 10.1, 8.4, 5.1, 7.7, 0, 8.1 y
9/15/2010 0:20, 8.8, 5.4, 7.6, 7.1, 0, 10.3 z
9/15/2010 0:30, 5.7, 8.8, 7.1, 1.6, 0, 6.8 ..
9/15/2010 0:50, 7.9, 9.2, 6.4, 6.2, 0, 8.4
9/15/2010 1:20, 0.6, 5.85, 8.1, 9.8, 0, 0.6 ....
Any help is highly appreciated
Thanks
I think your problem is the same as in: http://www.mathworks.com/support/solutions/en/data/1-143J0O/index.html?product=ML&solution=1-143J0O
d1=M;
d2=N;
newdat=[d1,zeros(length(d1(:,1)),length(d2(1,2:end)))]; %initialize new matrix with zeros
[m,n]=size(newdat); %get dimensions of new matrix
for i=1:length(d2(:,1)) %loop through data to merge
indx=find(newdat(:,1)==d2(i,1)); %find existing dates/labels
if indx
newdat(indx,[n-length(d2(i,2:end))+1:n])=d2(i,2:end); %if label is matched update the row
else
newdat(m+1,[n-length(d2(i,2:end))+1:n])=d2(i,2:end); %if the label is new, create new row
newdat(m+1,1)=d2(i,1); %insert new label in first column
m = m+1; %update the dimension of the new matrix
end
end
newdat=sortrows(newdat); %sort the new matrix
精彩评论