Video length is 6:06

What are Tables and How are They Used in MATLAB

Table arrays store column-oriented or tabular data, such as columns from a text file or spreadsheet.  Tables store each piece of column-oriented data in a variable which may contain different types of data. Learn how to take advantage of MATLAB tools and functionalities to perform everything from high-level analysis to complex functions on external table data such as Excel spreadsheets or tabular data.

Published: 3 Nov 2020

Welcome to using tables in MATLAB! In this video, we’ll define what a table is in MATLAB, how to import external table data into MATLAB from programs like Excel, and how to perform basic functions on the table using MATLAB.

Table arrays store column-oriented or tabular data, such as columns from a text file or spreadsheet.  Tables store each piece of column-oriented data in a variable which may contain different types of data.  For instance, tables may contain numerical data, alphanumerical or text strings, or categorical data.  In this video, we will focus on working with numerical and alphanumerical data.  

Tables are often found in formats such as spreadsheets or delimited-value files.  You’re probably familiar with tables in programs like Excel.  You can import, analyze, and extract table data in MATLAB without having to manually recreate a table from Excel.  Let’s look at an example.

Here, we have an Excel table with the average summer highs at MathWorks locations in North America.  You’ll see we have three different data types in the table: text strings, categorical, and numerical.  For this video, we will focus on working with alphanumeric and numerical data.

Let’s get this table imported into MATLAB.  

To import a table into MATLAB, click on “Import Data” in the top toolbar.

Navigate to where your table is located, select it, and click Open.

You’ll see that MATLAB is able to determine the table elements.  First, you’ll see the name of the table at the top based on the file name.  Second, you’ll see the table headers; these are called variable names in MATLAB.  You can adjust the row of variable names using the drop-down menu at the top.  Third, you’ll see the data types.  In this example, you’ll see locations represented as text data, time zones represented as categorical data, and temperatures represented as numerical data. 

If you want to change any data in the table, you can do so by double-clicking the corresponding cell .

If you want to change the data type of any variable, use the drop-down menu.

Once you are satisfied with the data you want to import, click Import Selection.  In MATLAB, you’ll see the table in the Workspace.

At any point, you can make changes to the table, by double-clicking on the table in the workspace to open the variable editor, selecting any of the cells, and making any necessary changes to its value.  You’ll notice that as you make any changes, it will display programmatically in the command window.

By right-clicking on aAny variable, you can sort the data as if you were in Excel, delete data, or export data into a separate table.  You can also add metadata to each variable.  

To obtain a high-level overview of table data, you can use the summary function to view a quick analysis of the table data by column.  For instance, we see there are three MathWorks locations in the Pacific Standard Time Zone, and the median average temperature in July of the MathWorks North America locations is 82 degrees.

You can do more than just analyze data using tables in MATLAB.  

You can use dot-indexing to perform functions on a single variable.  For example, if we want to find the mean (average) temperature of the North America locations in August, it can be easily done using the mean function.

Let’s assign meanaugtemp to equal the mean of August temperatures.  And you’ll see the average temperature in August of the North American locations is 81.5 degrees.

What if you want to focus on a specific variable of the table?  You can use curly braces ({}) to extract data from one variable into a single array.  For instance, let’s extract the July temperatures for further analysis.  We can extract the variable with the following syntax: SampleTable{1:5, ‘July’}

You can also concatenate the data further by extracting multiple variables.  If we wanted to extract July and August temperatures for further analysis, we would use the following syntax: SampleTable{1:5, ‘July’ ‘August’}.  This extracts the data into a single array.

If you want to extract specific variables of a table into a new table, you can do so with the following syntax using paranthesis: define the new table name = SampleTable(1:5, ‘July’ ‘August’) 

You can even add variables to a table using functions.  For instance, if we wanted to calculate the mean of each location’s summer temperature, we would have to add each average temperature – June, July, and August – and then divide by three.  In MATLAB, we would use the following syntax: SampleTable.meantemp = (SampleTable.June + SampleTable.July + SampleTable.August)/3.  We’ll see that it adds the new variable to the end of the table and performs the calculation.

There are many other functions you can perform on tables variables.  The MATLAB help documentation on tables and variables has lots of information, examples, and live scripts you can download to practice.  To access the help documentation, click on the question mark icon in the top right.

In this video, we covered how to define a table and the types of data tables contain, how to perform an analysis of the data, and how to perform basic functions.  Take advantage of MATLAB tools and functionalities to perform everything from high-level analysis to complex functions on external table data such as Excel spreadsheets or tabular data.  If you’re ready to take the next step using tables in MATLAB, we have more videos on importing Excel spreadsheets, performing complex table functions, and using categorical data in tables.