根据另一个文件中的匹配值过滤文本文件
Filter a text file based on matching values in another file
我想根据匹配的时间戳在 cygwin 或 matlab 中合并一些文本文件;
我有一个包含一列时间戳的文件:
文件 1:
7,
486,
730,
1031,
1331,
1631,
1931,
2231,
...
100000
和文件2;有 3 列和不同的时间戳(1hz):
1 data1 data2,
2 data1 data2,
3 data1 data2,
4 data1 data2,
....
100000 data1 data2
我想根据匹配的时间戳合并文件,以便保留文件 1,第 1 列:
7 data1 data2,
486 data1 data2,
730 data1 data2,
1031 data1 data2,
1331 data1 data2,
...
谢谢
您必须在 Matlab 中为您的案例使用的主要函数是:ismember
。
它将 return 文件 2(数据文件)中时间戳的索引(行号)与文件 1(仅时间戳)中的时间戳相匹配。
显然,先读取数据,合并,然后写入输出文件。
timestamp1 = dlmread('textfile1.txt') ; %// read timestamp only file
DATA = dlmread('textfile2.txt') ; %// read DATA file
timestamp2 = DATA(:,1) ; %// Extract timestamp from DATA (optional)
commonIndex = ismember(timestamp2,timestamp1) ; %// Find common timestamps line index in file 2
MergedData = DATA(commonIndex,:) ; %// build common matrix
dlmwrite('MergedData.txt',MergedData,'Delimiter',' ') %// write output file
我想根据匹配的时间戳在 cygwin 或 matlab 中合并一些文本文件;
我有一个包含一列时间戳的文件:
文件 1:
7,
486,
730,
1031,
1331,
1631,
1931,
2231,
...
100000
和文件2;有 3 列和不同的时间戳(1hz):
1 data1 data2,
2 data1 data2,
3 data1 data2,
4 data1 data2,
....
100000 data1 data2
我想根据匹配的时间戳合并文件,以便保留文件 1,第 1 列:
7 data1 data2,
486 data1 data2,
730 data1 data2,
1031 data1 data2,
1331 data1 data2,
...
谢谢
您必须在 Matlab 中为您的案例使用的主要函数是:ismember
。
它将 return 文件 2(数据文件)中时间戳的索引(行号)与文件 1(仅时间戳)中的时间戳相匹配。
显然,先读取数据,合并,然后写入输出文件。
timestamp1 = dlmread('textfile1.txt') ; %// read timestamp only file
DATA = dlmread('textfile2.txt') ; %// read DATA file
timestamp2 = DATA(:,1) ; %// Extract timestamp from DATA (optional)
commonIndex = ismember(timestamp2,timestamp1) ; %// Find common timestamps line index in file 2
MergedData = DATA(commonIndex,:) ; %// build common matrix
dlmwrite('MergedData.txt',MergedData,'Delimiter',' ') %// write output file