Join Multiple Tables horizontally (can have duplicate variables) in which some tables might be empty.

33 visualizaciones (últimos 30 días)
Hello All,
I want to Join Multiple Tables horizontally (can have duplicate variables) in which some tables might be empty.
How to do that ?
TIA!!!

Respuestas (1)

Benjamin Kraus
Benjamin Kraus el 22 de Feb. de 2023
Editada: Benjamin Kraus el 22 de Feb. de 2023
If your goal is to simply concatenate two tables, such that the first row of each table is combined into the first row of the output, the second row of each table is merged into the second row of the output, etc. all you need is to use the [] operator. That is, assuming they are the same height, or one of the tables is empty, and none of the tables have duplicate variable names. However, it is easy to fix duplicate variable names.
Consider this example:
t1 = table((1:10)')
t1 = 10×1 table
Var1 ____ 1 2 3 4 5 6 7 8 9 10
t2 = table((11:20)')
t2 = 10×1 table
Var1 ____ 11 12 13 14 15 16 17 18 19 20
t3 = table()
t3 = 0×0 empty table
% Append a prefix to table variable names to avoid duplicate names.
t1.Properties.VariableNames = "t1." + t1.Properties.VariableNames
t1 = 10×1 table
t1.Var1 _______ 1 2 3 4 5 6 7 8 9 10
t2.Properties.VariableNames = "t2." + t2.Properties.VariableNames
t2 = 10×1 table
t2.Var1 _______ 11 12 13 14 15 16 17 18 19 20
% Now concatenate the three matrices:
t = [t1 t2 t3]
t = 10×2 table
t1.Var1 t2.Var1 _______ _______ 1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 20
If your goal is more of a "join" operation, in which you use the value in one variable of each table as a key to match rows from multiple tables, you want to look into the different ways to Join Tables, including the innerjoin, outerjoin, and join commands.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by