How to expand a table with more columns?

9 views (last 30 days)
Leon
Leon on 30 Sep 2021
Commented: Leon on 30 Sep 2021
I have a table T1 with a size of 30000 x 55.
How do I add 6 more columns of this table: A, B, C, D, E, F, each of them have a size of 300000 x 1.
I tried to first form a new table
T2 = table(A, B, C, E, E, F);
and then join them together:
T_new = join(T1, T2);
Here is the error:
Error using tabular/join (line 130)
Cannot find a common table variable to use as a key variable.

Accepted Answer

Dave B
Dave B on 30 Sep 2021
Edited: Dave B on 30 Sep 2021
You can use the [ ] syntax to concatenate them, just like with a matrix (make sure that they don't share variable names):
a=rand(10,1);
b=rand(10,1);
T1=table(a,b)
T1 = 10×2 table
a b ________ ________ 0.015889 0.086344 0.22894 0.025285 0.22153 0.9393 0.82498 0.61865 0.91705 0.6433 0.41519 0.92073 0.33342 0.29499 0.83374 0.50394 0.60389 0.27498 0.98084 0.85301
c=rand(10,1);
d=rand(10,1);
T2=table(c,d)
T2 = 10×2 table
c d _______ ________ 0.73747 0.53457 0.55264 0.8379 0.50305 0.66591 0.95375 0.15684 0.18068 0.57932 0.6631 0.57374 0.48546 0.12394 0.15803 0.61982 0.12944 0.4348 0.62464 0.087705
T3=[T1 T2]
T3 = 10×4 table
a b c d ________ ________ _______ ________ 0.015889 0.086344 0.73747 0.53457 0.22894 0.025285 0.55264 0.8379 0.22153 0.9393 0.50305 0.66591 0.82498 0.61865 0.95375 0.15684 0.91705 0.6433 0.18068 0.57932 0.41519 0.92073 0.6631 0.57374 0.33342 0.29499 0.48546 0.12394 0.83374 0.50394 0.15803 0.61982 0.60389 0.27498 0.12944 0.4348 0.98084 0.85301 0.62464 0.087705
Alternatively, if you want to use join, you just need something to join them on (something which identfies which rows are the same in the two tables). You can use an existing index variable, or the RowNames property for this:
T1.Properties.RowNames=string(1:height(T1))';
T2.Properties.RowNames=string(1:height(T2))';
T4=join(T1,T2,'Keys','Row')
T4 = 10×4 table
a b c d ________ ________ _______ ________ 1 0.015889 0.086344 0.73747 0.53457 2 0.22894 0.025285 0.55264 0.8379 3 0.22153 0.9393 0.50305 0.66591 4 0.82498 0.61865 0.95375 0.15684 5 0.91705 0.6433 0.18068 0.57932 6 0.41519 0.92073 0.6631 0.57374 7 0.33342 0.29499 0.48546 0.12394 8 0.83374 0.50394 0.15803 0.61982 9 0.60389 0.27498 0.12944 0.4348 10 0.98084 0.85301 0.62464 0.087705

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!