table

Methods

Method Reference: table.outerjoin

table: tbl = outerjoin (tblL, tblR)
table: tbl = outerjoin (tblL, tblR, Name, Value)
table: [tbl, ixL, ixR] = outerjoin (…)

Outer join between two tables by rows using key variables.

tbl = outerjoin (tblL, tblR) combines the tables tblL and tblR by matching the values of their key variables, which by default are the variables that share the same name in both tables. Unlike innerjoin, an outer join also keeps the rows of each table that have no match in the other table, filling the variables taken from the non-matching table with missing values (NaN, NaT, <undefined>, empty string, etc., as appropriate). If m rows in tblL and n rows in tblR share the same key combination, then tbl contains all m×n pairings for that combination. The rows of tbl are sorted by the values of the key variables and any row names are dropped.

By default tbl contains all the variables of tblL followed by all the variables of tblR. Because the key variables are kept from both tables, conflicting names receive a suffix derived from each input’s argument name (for inputs named tblL and tblR, the suffixes '_tblL' and '_tblR'; when an input has no name, '_left' and '_right' are used). See 'MergeKeys' to combine the keys into single columns instead.

tbl = outerjoin (tblL, tblR, Name, Value) customizes the join with the following options:

'Type'
The type of outer join: 'full' (default) keeps unmatched rows from both tables, 'left' keeps all rows of tblL and only matching rows of tblR, and 'right' keeps all rows of tblR and only matching rows of tblL.
'MergeKeys'
A logical scalar (default false). When true, each pair of key variables is merged into a single variable that takes the value from tblL where a matching left row exists and from tblR otherwise. The merged variable is named after the left key when both keys share the same name, or 'leftName_rightName' when their names differ.
'Keys'
Variables to use as keys in both tables, given as variable names or indices. It cannot be combined with 'LeftKeys' or 'RightKeys'.
'LeftKeys', 'RightKeys'
Variables to use as keys in tblL and tblR, respectively, when the key variables have different names. They must be specified together and reference the same number of variables.
'LeftVariables', 'RightVariables'
Variables of tblL and tblR to include in tbl. By default all the variables of each table are included.

[tbl, ixL, ixR] = outerjoin (…) also returns the row-index vectors ixL and ixR that identify the row of tblL and tblR, respectively, corresponding to each row of tbl. A zero indicates a row of tbl that has no corresponding row in that table.

Source Code: table

Example: 1

outerjoin keeps all rows from both tables, filling in missing values where a key has no match — each variable is filled with the missing value proper to its type (NaN, <undefined>, empty string, ...).

 L = table ({'Li'; 'Diaz'; 'Brown'}, [38; 40; 49], ...
            'VariableNames', {'Name', 'Age'})
L =
  3x2 table

      Name       Age    
    _________    ___    

    {'Li'   }     38    
    {'Diaz' }     40    
    {'Brown'}     49
 R = table ({'Li'; 'Diaz'; 'Lee'}, categorical ({'NY'; 'CA'; 'TX'}), ...
            [176; 163; 150], ...
            'VariableNames', {'Name', 'State', 'Weight'})
R =
  3x3 table

      Name      State    Weight    
    ________    _____    ______    

    {'Li'  }       NY       176    
    {'Diaz'}       CA       163    
    {'Lee' }       TX       150
 outerjoin (L, R, 'MergeKeys', true)
ans =
  4x4 table

      Name       Age       State       Weight    
    _________    ___    ___________    ______    

    {'Brown'}     49    <undefined>       NaN    
    {'Diaz' }     40             CA       163    
    {'Lee'  }    NaN             TX       150    
    {'Li'   }     38             NY       176

Brown (left only) and Lee (right only) both survive, each with the other table's variables left missing.