Joining tables with different number of coloums

10 次查看(过去 30 天)
Hello!
i have a problem joining tables with the "join(Tleft,Tright, 'Keys', 'keyvar')" command.
merge_T1_T2 = join(T1, T2, 'Keys','SER_AKTUATOR');
Left and right key variables 'SER_AKTUATOR' and 'SER_AKTUATOR' do not have the same number of columns.
From my understanding there is no need for the number of coloums to be the same (the ML examples don't have this aswell).
I have already performed a sequence to allign the data types of the Keyvar (SER_AKTUATOR) and they are both 'Char'
Does anyone know how what the problem is/ ho to fix it?
Cheers Hannes
  2 个评论
Johannes Dechow
Johannes Dechow 2022-3-18
If i use double (original datatype of SER_AKTUATOR) i get a different Error:
The key variable for the right table must have unique values.
Arif Hoq
Arif Hoq 2022-3-18
attch your data and share the code that you have already tried and found the error

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2022-3-18
编辑:Stephen23 2022-3-18
"From my understanding there is no need for the number of coloums to be the same"
If the number of columns in the key variables are not the same then there a no matches and the whole exercise is moot.
"the ML examples don't have this aswell"
Yes they do: every single example on this page: https://www.mathworks.com/help/matlab/ref/table.join.html
uses key variables with exactly one column. The only variable on the entire page that consists of multiple columns is "BloodPressure", which is not used as a key variable.
As far as I can tell, you are confusing the number of columns in the tables (which is not important) with the number of columns in the key variable (which is what your error message is about).
Your error is easy to demonstrate:
X = ['A';'B';'C']; % one column
A = [111;123;456];
T1 = table(X,A)
T1 = 3×2 table
X A _ ___ A 111 B 123 C 456
X = ['C';'B']; % one column
B = [222;444];
T2 = table(X,B)
T2 = 2×2 table
X B _ ___ C 222 B 444
X = ['AA';'BB';'CC']; % two columns
C = [333;666;999];
T3 = table(X,C)
T3 = 3×2 table
X C __ ___ AA 333 BB 666 CC 999
join(T2,T1,'keys','X') % no problem: A and B have the same number of columns
ans = 2×3 table
X B A _ ___ ___ C 222 456 B 444 123
join(T3,T1,'keys','X') % error because B and C have different number of columns
Error using tabular/join
Left and right key variables 'X' and 'X' do not have the same number of columns.
"I have already performed a sequence to allign the data types of the Keyvar (SER_AKTUATOR) and they are both 'Char'"
And that is the cause of the problem.
"Does anyone know how what the problem is/ ho to fix it?"
Easy, simply replace that variable (a character array) with either of these:
  • a cell array of character vectors
  • a string array
with one column.
  4 个评论
Stephen23
Stephen23 2022-3-18
编辑:Stephen23 2022-3-18
"Since my understanding is that the whole point of joining tables is that they are common and not unique."
The key variable values must be unique in the right-table: "Each value must occur only once in the key variables of Tright", source: https://www.mathworks.com/help/matlab/ref/table.join.html
Writing "common" is insufficient to actually describe the behavior of JOIN. The definition of JOIN is actually "A table join appends rows from the right table where its key variables match values in the key variables of the left table".
It is not clear how joining would be defined if the values repeated in the right-table, because ultimately JOIN picks the the output rows depending on the value matching: if there are mulitple matches (as you propose), how would JOIN know which rows to pick from the right-table?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by