combining 4 csv files with and without headers

7 views (last 30 days)
I have 4 .csv files that is generated from an equipment that I am using. One of the csv files has 4 columns with a heading, and the other 3 csv files have between 2-3 columns without heading. I would like to use a column from each csv file as a reference as to where the values should be placed. The file with 4 colums is the "master" file with a timestamp for the duration of the experment and the other three csv files have unique timestamps that corrrespond to somewhere in the "master" file.
Is this something that is possilbe to do in Matlab?
  4 Comments
Adam Danz
Adam Danz on 27 Jul 2021
No, but you could attach files to a comment or, better yet, provide a quick sample of just a few rows from each of the 3 csv files and a description of how the data should be merged. Once the problem is clearly defined my bet is that the solution will be staightforward.
Terry Yin
Terry Yin on 27 Jul 2021
This is from the FP file
FrameCounter Timestamp Flags Region0
0 13084.4048 7 0.02967622
1 13084.4429 7 0.02961321
2 13084.481 7 0.02967732
The Timestamp that is marked originates from the time when the machine was turned on. While other files generated from the file is based of milliseconds of the day. So there is another file that looks at the frame (column 1) and pairs it with the time of the day, and this file has no heading.
0 48786598
1 48786634.2
2 48786672.8
3 48786710.3
There is also another file generated based on a keydown that I hit. The timestamp is based off the "time of day", which is why we have to convert the "frame counter" to a new timestamp. This will only show something if I press the space bar, and this file has no header either.
Space 49154818.5
Space 49210956.1
Space 49218482.5
Space 49253519.6
Basically what I want to do is combine the first two files together so I have a reference point where I can designate where the keydown was pushed to idenitfy some response.
Hope this helps clarify my issue (there is another file that is generated without a heading, but is not important to the experiment that I am conducting)

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 27 Jul 2021
You can use outerjoin to join the first 2 tables into 1 table. It's not entirely clear how you're indexing the 3rd table into the first two but it sounds like you want the closest time stamps in table 3 to the time stamps in table 2. These two steps are demonstracted below using the demo data you provided.
% Create 3 tables
d1 = [ 0 13084.4048 7 0.02967622
1 13084.4429 7 0.02961321
2 13084.481 7 0.02967732 ];
T1 = array2table(d1, 'VariableNames', {'FC','timestamp','Flags','Region'})
T1 = 3×4 table
FC timestamp Flags Region __ _________ _____ ________ 0 13084 7 0.029676 1 13084 7 0.029613 2 13084 7 0.029677
d2 = [ 0 48786598
1 48786634.2
2 48786672.8
3 48786710.3 ];
T2 = array2table(d2, 'VariableNames', {'FC','time_ms'})
T2 = 4×2 table
FC time_ms __ __________ 0 4.8787e+07 1 4.8787e+07 2 4.8787e+07 3 4.8787e+07
d3 = { 'Space' 49154818.5
'Space' 49210956.1
'Space' 49218482.5
'Space' 49253519.6};
T3 = cell2table(d3, 'variablenames', {'Key','time_ms'})
T3 = 4×2 table
Key time_ms _________ __________ {'Space'} 4.9155e+07 {'Space'} 4.9211e+07 {'Space'} 4.9218e+07 {'Space'} 4.9254e+07
% Join tables 1 and 2
T12 = outerjoin(T1, T2, 'keys','FC','MergeKeys',true)
T12 = 4×5 table
FC timestamp Flags Region time_ms __ _________ _____ ________ __________ 0 13084 7 0.029676 4.8787e+07 1 13084 7 0.029613 4.8787e+07 2 13084 7 0.029677 4.8787e+07 3 NaN NaN NaN 4.8787e+07
% Find the row of the closest time stamples in T12 to the time stamps in T3
[~, minidx] = min(abs(T12.time_ms - T3.time_ms'))
minidx = 1×4
4 4 4 4
T12(minidx,:)
ans = 4×5 table
FC timestamp Flags Region time_ms __ _________ _____ ______ __________ 3 NaN NaN NaN 4.8787e+07 3 NaN NaN NaN 4.8787e+07 3 NaN NaN NaN 4.8787e+07 3 NaN NaN NaN 4.8787e+07

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!