How to merge two tables into a single table?
17 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
採用された回答
Stephen23
2024 年 10 月 14 日
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
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
Use curly-brace indexing to implicitly create the new column:
table1{:,'Source'} = "table1";
table2{:,'Source'} = "table2";
table3 = [table1;table2]
その他の回答 (2 件)
Aquatris
2024 年 10 月 14 日
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]
Rahul
2024 年 10 月 14 日
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)
For more information regarding the functions mentioned above, refer to the following documentation links:
- Repeat copies of array using “repmat” function: https://www.mathworks.com/help/matlab/ref/repmat.html
- Concatenate arrays vertically using “vertcat” function: https://in.mathworks.com/help/matlab/ref/double.vertcat.html
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!