table

Methods

Method Reference: table.horzcat

table: tbl = horzcat (tbl1, tbl2, …)

Horizontal concatenation for tables.

tbl = horzcat (tbl1, tbl2, …) merges tables by horizontally concatenating them, provided that all input tables have collectively unique variable names and the same number of rows.

Input tables that have row names must share the same unique set of row names but not necessarily in the same order. When row names are present in multiple input tables, their position is matched to the row names of the first input table. Input tables without row names are concatenated by position without re-indexing. Output table’s Description and UserData properties are assigned using the first non-empty value.

Source Code: table

Example: 1

horzcat joins tables side by side, keeping all their variables. The tables must have the same number of rows and distinct variable names. The [A, B] bracket syntax calls it for you, and variables of any type combine freely.

 A = table ([38; 43; 40], 'VariableNames', {'Age'})
A =
  3x1 table

    Age    
    ___    

     38    
     43    
     40
 B = table (categorical ({'M'; 'F'; 'M'}), logical ([1; 0; 1]), ...
            'VariableNames', {'Gender', 'Smoker'})
B =
  3x2 table

    Gender    Smoker    
    ______    ______    

         M    true      
         F    false     
         M    true
 [A, B]
ans =
  3x3 table

    Age    Gender    Smoker    
    ___    ______    ______    

     38         M    true      
     43         F    false     
     40         M    true

Example: 2

Custom properties are carried through the concatenation. A variable-scoped property is concatenated alongside its columns, so the merged table keeps one entry per variable; a table-scoped property is preserved when the operands agree on it.

 A = table ([38; 43], categorical ({'M'; 'F'}), 'VariableNames', {'Age', 'Gender'})
A =
  2x2 table

    Age    Gender    
    ___    ______    

     38         M    
     43         F
 A = addprop (A, {'Study', 'Source'}, {'table', 'variable'});
 A.Properties.CustomProperties.Study = 'Cohort-7';
 A.Properties.CustomProperties.Source = {'intake', 'intake'};
 B = table (duration (0, [15; 30], 0), 'VariableNames', {'Wait'})
B =
  2x1 table

      Wait      
    ________    

    00:15:00    
    00:30:00
 B = addprop (B, {'Study', 'Source'}, {'table', 'variable'});
 B.Properties.CustomProperties.Study = 'Cohort-7';
 B.Properties.CustomProperties.Source = {'clock'};
 H = [A, B];

The variable-scoped Source now spans all three variables ...

 H.Properties.CustomProperties.Source
ans =
  1x3 cell array

    {'intake'}    {'intake'}    {'clock'}

... and the shared table-scoped Study is retained.

 H.Properties.CustomProperties.Study
ans = Cohort-7