Trying to merge two tables, using time stamps as keys in outerjoin(), getting 'incorrect data type or missing argument' error.

2 views (last 30 days)
Hi!
I have two tables, each with two rows. One is 'raw', and has each possible time stamp and corresponding data. The other is 'valid', and only has time stamps corresponding to valid data. I want to have a third table, that contains all time stamps but NaNs where there isn't both raw and valid data.
So, for example, these would go into outerjoin()
valid = 't_ms' 'val_dat'
[ 1 55
2 50
4 52 ]
raw = [ 1 49
2 37
3 51
4 52 ]
And I'd get a table that's the length of 'raw', and 'valid' containing NaNs. Instead, I get the error:
'Check for incorrect argument data type or missing argument in call to outerjoin.'
I've quadruple checked that I'm inputting tables, and the key variables have the exact same name, and I've tried pretty much every optional argument into outerjoin() I can, no dice.
Any help would be much appreciated, embarrassingly enough I'm stumped!
Thanks

Answers (2)

Xiaotao
Xiaotao on 22 May 2024
valid = table([1;2;4],[55 50 52]', 'VariableNames',{'t_ms','val_dat'});
raw = table([1;2;3;4],[49 37 51 52]', 'VariableNames',{'t_ms','raw_dat'});
third = outerjoin(valid, raw,'MergeKeys',true)
third = 4x3 table
t_ms val_dat raw_dat ____ _______ _______ 1 55 49 2 50 37 3 NaN 51 4 52 52

Abhas
Abhas on 22 May 2024
Hi Tyler,
MATLAB's "outerjoin" function can be used with "MergeKeys" arguments to fill the missing values with NaNs where there is no match.
Here's the MATLAB code to perform the required steps:
% Define the 'valid' table
valid = table([1; 2; 4], [55; 50; 52], 'VariableNames', {'t_ms', 'val_dat'});
% Define the 'raw' table
raw = table([1; 2; 3; 4], [49; 37; 51; 52], 'VariableNames', {'t_ms', 'raw_dat'});
% Perform the outer join
result = outerjoin(raw, valid, 'Keys', 't_ms', 'MergeKeys', true)
result = 4x3 table
t_ms raw_dat val_dat ____ _______ _______ 1 49 55 2 37 50 3 51 NaN 4 52 52
The "result" variable stores the "outerjoin" of the two tables.
You may refer to the following documentation link to have a better understanding on joins:
  1. https://www.mathworks.com/help/matlab/ref/table.outerjoin.html
  2. https://www.mathworks.com/help/matlab/ref/table.join.html

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!