我有多个文件长度不同的文件。
文件1是长度为86400 X 10的.txt文件 文件2是长度为144×10的.csv文件
现在这是两个文件都包含所有数据的情况,但很多时候我在中间缺少时间戳,所以file1的长度是1000-86400 X 10 文件2是10-144×10
现在我要编写长度为144 X 20的第三个文件,以包含文件2和文件1的所有列(我将86400平均减少到144)。
我的问题是我无法正确匹配时间来创建第三个文件 有什么帮助吗?
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
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
我用来读取文件
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}]
如何使用与文件2格式相同的匹配时间戳写入包含文件1和文件2的所有列的第3个文件?
我正在尝试将第三个文件写为
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 ....
非常感谢任何帮助
由于
答案 0 :(得分:0)
我认为您的问题与: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