how to merge two tables with same column names into one table?

46 views (last 30 days)
I have two table with the same column names T1 and T2,
T1 = array2table(rand(4,3), 'VariableNames', {'A', 'B', 'C'})
T1 =
4×3 table
A B C
_______ _______ _______
0.3111 0.90488 0.25806
0.92338 0.97975 0.40872
0.43021 0.43887 0.5949
0.18482 0.11112 0.26221
T2 = array2table(rand(4,3), 'VariableNames', {'A', 'B', 'C'})
T2 =
4×3 table
A B C
_______ _______ ________
0.60284 0.29668 0.085516
0.71122 0.31878 0.26248
0.22175 0.42417 0.80101
0.11742 0.50786 0.02922
how can I merge them into one table T3?
T3 = merge(T1, T2)
T3 =
4×3 table
A B C
________________ _________________ __________________
0.3111 0.60284 0.90488 0.29668 0.25806 0.085516
0.92338 0.71122 0.97975 0.31878 0.40872 0.26248
0.43021 0.22175 0.43887 0.42417 0.5949 0.80101
0.18482 0.11742 0.11112 0.50786 0.26221 0.02922

Answers (1)

KSSV
KSSV on 10 May 2021
T1 = array2table(rand(4,3), 'VariableNames', {'A', 'B', 'C'}) ;
T2 = array2table(rand(4,3), 'VariableNames', {'A', 'B', 'C'}) ;
T = [T1 ;T2]
  1 Comment
Longshan Du
Longshan Du on 10 May 2021
In this way, T will be a 8x3 table, not the same as T3, which is a 4x3 table
T =
8×3 table
A B C
_______ ________ ________
0.92885 0.23728 0.52114
0.73033 0.45885 0.23159
0.48861 0.96309 0.4889
0.57853 0.54681 0.62406
0.67914 0.037739 0.098712
0.39552 0.88517 0.26187
0.36744 0.91329 0.33536
0.98798 0.79618 0.67973
The column with same name should be merged together, not concatenate vertically.

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!