How to merge two tables into a single table?

13 views (last 30 days)
Hi all!
I may ask for a simple task, but im stuck at this point.
So, let's imagine I have two tables randomnly generated as follow:
table1 = table(["Smith";"Nguyen";"Williams";"Fernandez";"Brown"],[38;43;38;40;49], ...
[71;69;64;67;64],[176;163;131;133;119]) % Randomly generated table1
table1.Properties.VariableNames = ["LastName","Age","Height","Weight"] % Adding headers
table2 = table(["James";"Mary";"Anna";"Jhon";"Pow"],[67;49;18;95;29;67;36], ...
[31;65;23;17;48];90,[16;63;13;33;1;765]) % Randomly generated table2
table2.Properties.VariableNames = ["LastName","Age","Height","Weight"] % Adding headers
I'd like to merge table1 and tabl 2 into one single table (let's call it table3), but indicating in the table3 header row the original table from which the data belong (this is, maybe specifying table1 and table2 across one single column, as a variable name).
If needed, I'm using R2022b MATLAB version.
I really appreciate your help. Best regards!

Accepted Answer

Stephen23
Stephen23 on 14 Oct 2024
Assuming that the tables already exist:
table1 = table(["Smith";"Nguyen";"Williams";"Fernandez";"Brown"],[38;43;38;40;49], ...
[71;69;64;67;64],[176;163;131;133;119]); % Randomly generated table1
table1.Properties.VariableNames = ["LastName","Age","Height","Weight"] % Adding headers
table1 = 5x4 table
LastName Age Height Weight ___________ ___ ______ ______ "Smith" 38 71 176 "Nguyen" 43 69 163 "Williams" 38 64 131 "Fernandez" 40 67 133 "Brown" 49 64 119
table2 = table(["James";"Mary";"Anna";"Jhon";"Pow"],[67;49;18;95;29],...
[31;65;23;17;48],[90;16;63;13;33]); % Randomly generated table2
table2.Properties.VariableNames = ["LastName","Age","Height","Weight"] % Adding headers
table2 = 5x4 table
LastName Age Height Weight ________ ___ ______ ______ "James" 67 31 90 "Mary" 49 65 16 "Anna" 18 23 63 "Jhon" 95 17 13 "Pow" 29 48 33
Use curly-brace indexing to implicitly create the new column:
table1{:,'Source'} = "table1";
table2{:,'Source'} = "table2";
table3 = [table1;table2]
table3 = 10x5 table
LastName Age Height Weight Source ___________ ___ ______ ______ ________ "Smith" 38 71 176 "table1" "Nguyen" 43 69 163 "table1" "Williams" 38 64 131 "table1" "Fernandez" 40 67 133 "table1" "Brown" 49 64 119 "table1" "James" 67 31 90 "table2" "Mary" 49 65 16 "table2" "Anna" 18 23 63 "table2" "Jhon" 95 17 13 "table2" "Pow" 29 48 33 "table2"
  1 Comment
Sara Woods
Sara Woods on 14 Oct 2024
Thanks so much! Indeed, my problem was adding a column to an already existing table!

Sign in to comment.

More Answers (2)

Aquatris
Aquatris on 14 Oct 2024
You can add "Source" header to each table with "Table 1" or "Table 2" inputs and join them like joining arrays;
table1 = table(repelem("Table 1",5,1),["Smith";"Nguyen";"Williams";"Fernandez";"Brown"],[38;43;38;40;49], ...
[71;69;64;67;64],[176;163;131;133;119]); % Randomly generated table1
table1.Properties.VariableNames = ["Source","LastName","Age","Height","Weight"]; % Adding headers
table2 = table(repelem("Table 2",5,1),["James";"Mary";"Anna";"Jhon";"Pow"],[67;49;18;95;29], ...
[31;65;23;17;48],[16;63;13;33;176]); % Randomly generated table2
table2.Properties.VariableNames = ["Source","LastName","Age","Height","Weight"]; % Adding headers
table3 = [table1;table2]
table3 = 10x5 table
Source LastName Age Height Weight _________ ___________ ___ ______ ______ "Table 1" "Smith" 38 71 176 "Table 1" "Nguyen" 43 69 163 "Table 1" "Williams" 38 64 131 "Table 1" "Fernandez" 40 67 133 "Table 1" "Brown" 49 64 119 "Table 2" "James" 67 31 16 "Table 2" "Mary" 49 65 63 "Table 2" "Anna" 18 23 13 "Table 2" "Jhon" 95 17 33 "Table 2" "Pow" 29 48 176
  1 Comment
Sara Woods
Sara Woods on 14 Oct 2024
Thanks! But, there is another way to add the header row "source" to table1 and table2 straightforwardly? This is, in another code line, not in the code line generating the tables

Sign in to comment.


Rahul
Rahul on 14 Oct 2024
I’m assuming that you’re trying to vertically concatenate two tables in MATLAB, into a new table, which contains an additional column stating the origin table of that specific row.
You can include an additional ‘origin’ column in both tables, which can later be incorporated in the concatenated table, showing the source table of that entry. Here’s how you can structure your code, to include the 'origin' column at the time of table generation:
% Generate table1 with a 'Source' column
table1 = table(["Smith";"Nguyen";"Williams";"Fernandez";"Brown"], [38;43;38;40;49], ...
[71;69;64;67;64], [176;163;131;133;119], ...
repmat("table1", 5, 1), 'VariableNames', ...
["LastName", "Age", "Height", "Weight", "Source"]);
% Generate table2 with a 'Source' column
table2 = table(["James";"Mary";"Anna";"John";"Pow"], [67;49;18;95;29], ...
[31;65;23;17;48], [90;16;63;13;33], ...
repmat("table2", 5, 1), 'VariableNames', ...
["LastName", "Age", "Height", "Weight", "Source"]);
% Merge the two tables vertically
table3 = vertcat(table1, table2)
table3 = 10x5 table
LastName Age Height Weight Source ___________ ___ ______ ______ ________ "Smith" 38 71 176 "table1" "Nguyen" 43 69 163 "table1" "Williams" 38 64 131 "table1" "Fernandez" 40 67 133 "table1" "Brown" 49 64 119 "table1" "James" 67 31 90 "table2" "Mary" 49 65 16 "table2" "Anna" 18 23 63 "table2" "John" 95 17 13 "table2" "Pow" 29 48 33 "table2"
For more information regarding the functions mentioned above, refer to the following documentation links:
  1 Comment
Sara Woods
Sara Woods on 14 Oct 2024
Thanks! But, if I want to add a header 'Source' with my desired variable 'table1' to the already existing table1, how could I code it? Thanks again!

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!