Main Content

Join Tables

Combine two tables using key variables in the Live Editor

Since R2019b

Description

The Join Tables task lets you interactively combine two tables by performing joins or by concatenating the tables horizontally or vertically. The task automatically generates MATLAB® code for your live script.

Using this task, you can:

  • Perform joins, including inner and outer joins, on tables by specifying key variables—table variables whose matching values, or key values, determine which rows are merged into the output table.

  • Horizontally or vertically concatenate tables with the same number of rows or variables, respectively.

Join Tables task in Live Editor

Open the Task

To add the Join Tables task to a live script in the MATLAB Editor:

  • On the Live Editor tab, select Task > Join Tables.

  • In a code block in the script, type a relevant keyword, such as join or tables. Select Join Tables from the suggested command completions.

Examples

expand all

This example shows how to use the Join Tables Live Editor task to perform an inner join and an outer join on two tables.

First, load the orders table, which has order IDs, customer names, and the order date for a number of shipments.

load orders

Then load the items table, which contains products that customers ordered, along with the price and status of the shipment for that item. Each row of this table has an order ID, just like orders. Because a customer can order multiple items, several rows of items can refer to one order from orders.

load items

Open the Join Tables task. To open the task, type the keyword join in a code block and click Join Tables when it appears in the menu.

Use the task to perform an inner join of orders and items. When the task opens:

  1. Select orders and items as the left and right tables, respectively.

  2. Select OrderID as the merging variable for both tables.

  3. Click the Inner join button.

  4. To see the code that this task generates, click the small arrow under the Display results section. In this example, the generated code is a call to the innerjoin function.

When you perform an inner join, the output table includes only those key values that appear in both the left and right tables.

  • Only the values 1037 and 5120 occur in both input tables. Therefore, the output table contains data for only those two orders.

  • Value 1037 occurs multiple times in items, once for each item, but only once in orders. Therefore, the CustomerID and OrderDate values corresponding to 1037 are copied as many times as needed in the output.

Next, use the task to perform a left outer join. Outer joins can include key values that appear in only one input table. For example, a left outer join includes all key values from the left table, even when the right table has no corresponding matches. If the right table has key values that do not have matches in the left table, then those key values are not included.

  1. Click the Left outer join button.

  2. Select the Combine merging variables check box. By default, outer joins copy the key variables from the left and right tables into separate variables in the output table. Merge the key variables so one key variable is in the output.

  3. To see the generated call to the outerjoin function, click the small arrow under Display results.

The output table now includes data for order 8937. However, because the items table for order 8937 had no items, the rest of the row is filled in with empty values (such as '', NaN, or <undefined>). Outer joins fill table elements with empty values when the left or right tables do not have data associated with a key value.

  • Full outer joins include all key values and data from both tables.

  • Left outer joins include all key values and data from the left table, but only matching key values and associated data from the right.

  • Right outer joins include all key values and data from the right table, but only matching key values and associated data from the left.

Related Examples

Parameters

expand all

Specify the name from a list of all the nonempty tables and timetables that are in the workspace.

Specify the name from a list of all the nonempty tables and timetables that are in the workspace.

Specify the name of a variable from a list of variables in the left or right table.

  • The drop-down list for the left table contains a full list of its variables.

  • The drop-down list for the right table contains a subset of variables. The list of merging variables from the right table is constrained by your choice of merging variables from the left table.

When you specify a merging, or key, variable, its values determine which rows are merged from the left and right tables. To specify multiple sets of merging variables, use the + button.

Combine corresponding merging variables when performing outer joins. By default, outer joins copy key variables from the left and right tables to their own variables in the output table. To combine corresponding key variables in the left and right tables into one variable in the output, select this check box.

Version History

Introduced in R2019b

expand all