MATLAB Answers

How concatenate two tables in MATLAB?

885 views (last 30 days)
Adriano
Adriano on 19 Nov 2016
Commented: Peter Perkins on 30 Apr 2020
I have two tables with no variables in common, with the same number of rows and with different number of columns. For example:
Age Height
___ ______
38 71
43 69
and the table:
Weight Blood
______ ______
176 124
163 109
How can I get:
Age Height Weight Blood
___ ______ ______ _____
38 71 176 124
43 69 163 109
Thanks!

  0 Comments

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 23 Nov 2016
Adriano, to horizontally concatenate anything in MATLAB, you would probably just use square brackets, right? Such as AB = [A B]. Same for tables. cat and horzcat provide a functional form of that, but the standard MATLAB way is brackets.

  2 Comments

Johanne Lebrun Thauront
Johanne Lebrun Thauront on 30 Apr 2020
Hello,
Not sure someone will see that 4 years later but I try just in case :)
This doesn't work for me, all the variables of the second table are merged into a single variable with multiple columns in the concatenated table.
For instance I tried to concatenate two tables A (size 32x4) and B (size 32x50) and I ended up with AB (size 32x5) where the fifth variable has 50 columns!....
Any way to get AB as a 32x54 table?
Thanks in advance!
Johanne
Peter Perkins
Peter Perkins on 30 Apr 2020
Horizontal concatenation of tables will not merge variables. You need to say exactly what you started from and what you did.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!