How to combine table vertically

78 次查看(过去 30 天)
I have two large tables I want to combine. They both have exactly the same column headings but different row numbers (>300) and the headings may be numbers, categoricals or text. I just want to combine them vertically so that the final table has just one column heading with all the data underneath.
  2 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2018-11-24
outerjoin(table1,table2,'MergeKeys', true);
Ans Link
Wendy Cameron
Wendy Cameron 2018-11-24
Thank you - this answer worked. I had been trying to use vertcat which works for an array and although one could convert the table to an arry as below, this solution here seems to work nicely in my situation. Thank you all very much or your ongoing assistance with Matlab.
Kind regards, Wendy

请先登录,再进行评论。

采纳的回答

madhan ravi
madhan ravi 2018-11-24
编辑:madhan ravi 2018-11-24

更多回答(1 个)

Peter Perkins
Peter Perkins 2018-11-27
Wendy, I'm not sure you are getting the correct advice. You have said you want to vertically concatenate. outerjoin does not do that. It's a completely different operation, closer to horizontal concatenation.
If you do want to vertically concatenate, you do that the same way with tables as with anything else in MATLAB: [t1; t2]. No conversion needed. However, the variable names in the two tables do need to be the same. You have said, "They both have exactly the same column headings", so that should not be a problem. You also said, "the headings may be numbers, categoricals or text.", which doesn't make sense, so I think maybe you meant to say, "the variables in the table may be ...".
So, if you want to vertically concatenate, you shouldn't use outerjoin, and you don't need to convert back and forth with numeric.
  3 个评论
Peter Perkins
Peter Perkins 2018-11-27
I almost take back part of what I said about outerjoin. outerjoin happens to work in this case, because
1) you have identical variable names in both tables, and the default behavior is to use common vars as keys,
2) there's the flag to merge the keys in the output, and
3) apparently the two tables have no rows in common, so the outer join doesn't actually join anything, it treats every row as unmatched.
But this is pretty obscure. It will not work correctly as soon as the two tables have a row in common, and it isn't very obvious. A join is fundamentally a horzcat-like operation. What went wrong with vertcat?
On the other hand, if what you really want is, "vertcat the two tables but only keep one copy of common rows", then perhaps outerjoin is what you want. I would argue that vertcat+unique is more understandable, but that's up to you.
Hope this helps.
Wendy Cameron
Wendy Cameron 2018-11-27
Great, thanks for the feedback Peter. It seems it worked just out of luck for my situation. I checked a few columns to be sure. But your method works too and is of course simpler and I have noted that down to use next time. Thanks once again for helping me to better understand matlab.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by