How to concatenate two tables in MATLAB

Viewed 530

I am reading from two txt files. I have imported them into Matlab and did some calculations, this gave me two tables. Is it possible to concatenate the two tables into one? I am using version 2012b and I have tried join(LeftT, RightT) but I get Undefined function or variable Please see below:

LeftT:

1936
1937
1938

RightT:

5.4000    5.5000    5.3000    8.5000    8.7000    7.9000    8.3000    8.7000    7.5000    6.5000    5.5000    5.3000
3.6000    6.0000    6.7000    8.0000    9.7000    9.5000    9.4000    6.9000    7.6000    7.3000    5.5000    5.5000
5.7000    5.1000    7.4000    7.8000    9.5000    9.7000    8.9000    7.9000    7.2000    6.4000    6.5000    5.2000

Expected:

1936    5.4000    5.5000    5.3000    8.5000    8.7000    7.9000    8.3000    8.7000    7.5000    6.5000    5.5000    5.3000
1937    3.6000    6.0000    6.7000    8.0000    9.7000    9.5000    9.4000    6.9000    7.6000    7.3000    5.5000    5.5000
1938    5.7000    5.1000    7.4000    7.8000    9.5000    9.7000    8.9000    7.9000    7.2000    6.4000    6.5000    5.2000

Code:

LeftT = TableMin(:,1:13);
RightT = TableMax(:,1:13);

DifferenceT = RightT(:,2:13)-LeftT(:,2:13)
Results = Years , DifferenceT
1 Answers

If this is the same data as yesterday, your data is not really in a MATLAB table. MATLAB has a specific data structure called table, but for your data you are just concatenating matrices.

To stack matrices A and B side by side, use a space or comma:

[A, B] % comma optional

To stack vertically, use a semicolon:

[A; B]

To put the data into a MATLAB table, use splitvars:

T = splitvars(table(LeftT, RightT))

% T =
%   3×13 table
%     LeftT    RightT_1    RightT_2    RightT_3    RightT_4    RightT_5    RightT_6    RightT_7    RightT_8    RightT_9    RightT_10    RightT_11    RightT_12
%     _____    ________    ________    ________    ________    ________    ________    ________    ________    ________    _________    _________    _________
% 
%     1936       5.4         5.5         5.3         8.5         8.7         7.9         8.3         8.7         7.5          6.5          5.5          5.3   
%     1937       3.6           6         6.7           8         9.7         9.5         9.4         6.9         7.6          7.3          5.5          5.5   
%     1938       5.7         5.1         7.4         7.8         9.5         9.7         8.9         7.9         7.2          6.4          6.5          5.2   

To get the row with the max value:

[MaxValue, MaxRow] = max(max(T{:, 2:end}, [], 2));

Year = T{MaxRow, 1}
Values = T{MaxRow, 2:end}

% Year =
%     1937
% 
% Values =
%     3.6000    6.0000    6.7000    8.0000    9.7000    9.5000    9.4000    6.9000    7.6000    7.3000    5.5000    5.5000
Related