How to merge or combine 2 datasets with different number of rows and different name of columns

6 ビュー (過去 30 日間)
Ngoc Nguyen
Ngoc Nguyen 2022 年 5 月 6 日
回答済み: Tejas 2025 年 2 月 24 日
  2 件のコメント
Raju
Raju 2022 年 5 月 6 日
Hi Ngoc,
You can make use of the table & joindata commands. Below is the similar example which you can useto achieve the same. Make sure you maintain the common id to join the data, In this case 151.04 & 151.08 have the same id 1 & 2 in both the table before joining it
table1=table([1;2;3],[151.04;151.08;151.11], [3.2634e+05;1.6518e+05;1.1548e+05], 'VariableNames', ["id", "mz", "sp58"]);
table2=table([0;1;2;4],[150.09;151.04;151.08;151.09], [217504.6;122152.8;561438.7;88868.3], 'VariableNames', ["id", "mz", "sp59"]);
table12 = joindata(table2,table1, 'Keys', ["id", "mz"]);
Thanks & Regards,
Raju
Ngoc Nguyen
Ngoc Nguyen 2022 年 5 月 10 日
Dear Raju,
Thank you very much for your comment!
I tried the codes you suggested but the outcome has an error:
>> table1=table([1;2;3],[151.04;151.08;151.11], [3.2634e+05;1.6518e+05;1.1548e+05], 'VariableNames', ["id", "mz", "sp58"]);
table2=table([0;1;2;4],[150.09;151.04;151.08;151.09], [217504.6;122152.8;561438.7;88868.3], 'VariableNames', ["id", "mz", "sp59"]);
table12 = joindata(table2,table1, 'Keys', ["id", "mz"]);
Undefined function or variable 'joindata'.
I am using MATLAB (individual) version 2018b for academic use. I am still struggling with this error and trying to understand the reason before successfully applying your solution.
Thank you for your consideration!
Best regards,
Ngoc

サインインしてコメントする。

回答 (1 件)

Tejas
Tejas 2025 年 2 月 24 日
Hello Ngoc,
To merge two tables that share a common column name, use the 'outerjoin' function. Here is a sample code snippet demonstrating how to use the function:
table1 = table([151.05; 151.08; 151.11], [3.2634e+05; 1.6518e+05; 1.1548e+05], ...
'VariableNames', {'mz', 'sp58'});
table2 = table([150.09; 151.04; 151.08; 151.09], [2571014.6; 122153.8; 561438.7; 88868.3], ...
'VariableNames', {'mz', 'sp59'});
mergedTable = outerjoin(table1, table2, 'Keys', 'mz', 'MergeKeys', true, 'Type', 'full');
disp(mergedTable);
For more information on the 'outerjoin' function, use the command below to access the documentation:
>> web(fullfile(docroot, "matlab/ref/table.outerjoin.html"))

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by