I would like to merge some text files in cygwin or matlab based on matching timestamps;
I have one file with a column of time stamps:
file 1:
7,
486,
730,
1031,
1331,
1631,
1931,
2231,
...
100000
and file 2; with 3 columns and a different time stamp(1hz) :
1 data1 data2,
2 data1 data2,
3 data1 data2,
4 data1 data2,
....
100000 data1 data2
I would like to merge the files based on matching timestamps such that I preserve file1, column 1:
7 data1 data2,
486 data1 data2,
730 data1 data2,
1031 data1 data2,
1331 data1 data2,
...
thanks
The main function you have to use in Matlab for your case is : ismember
.
It will return the indices (line number) of the timestamp in file 2 (data file) which match the timestamps from file 1 (timestamp only).
obviously, read your data first, do you merging, then write the output file.
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