Advantages of Using Tables

Conveniently Store Mixed-Type Data in Single Container

You can use the table data type to collect
mixed-type data and metadata properties, such as variable name, row
names, descriptions, and variable units, in a single container. Tables
are suitable for column-oriented or tabular data that is often stored
as columns in a text file or in a spreadsheet. For example, you can
use a table to store experimental data, with rows representing different
observations and columns representing different measured variables.

Tables consist of rows and column-oriented variables. Each variable
in a table can have a different data type and a different size, but
each variable must have the same number of rows.

For example, load sample patients data.

load patients

Then, combine the workspace variables, Systolic and Diastolic into
a single BloodPressure variable and convert the
workspace variable, Gender, from a cell array of
strings to a categorical array.

The table displays in a tabular format with the variable names
at the top.

Each variable in a table is a single data type. For example,
if you add a new row to the table, MATLAB® forces consistency
of the data type between the new data and the corresponding table
variables. If you try to add information for a new patient where the
first column contains the patient's age instead of gender,
you receive an error.

Fields in a structure array are analogous to variables in a
table. However, unlike with tables, you cannot enforce homogeneity
within a field. For example, you can have some values of S.Gender that
are categorical array elements, 'Male' or 'Female',
others that are strings, 'Male' or 'Female',
and others that are integers, 0 or 1.

Now consider the same data stored in a scalar structure,
with four fields each containing one variable from the table.

Unlike with tables, you cannot enforce that the data is rectangular.
For example, the field ScalarStruct.Age can be
a different length than the other fields.

A table allows you to maintain the rectangular structure (like
a structure array) and enforce homogeneity of variables (like fields
in a scalar structure). Although cell arrays do not have named fields,
they have many of the same disadvantages as structure arrays and scalar
structures. If you have rectangular data that is homogeneous in each
variable, consider using a table. Then you can use numeric or named
indexing, and you can use table properties to store metadata.

Access Data Using Numeric or Named Indexing

You can index into a table using parentheses, curly braces,
or dot indexing. Parentheses allow you to select a subset of the data
in a table and preserve the table container. Curly braces and dot
indexing allow you to extract data from a table. Within each table
indexing method, you can specify the rows or variables to access by
name or by numeric index.

Consider the sample table from above. Each row in the table, T,
represents a different patient. The workspace variable, LastName,
contains unique identifiers for the 100 rows. Add row names to the
table by setting the RowNames property to LastName and
display the first five rows of the updated table.

In addition to labeling the data, you can use row and variable
names to access data in the table. For example, use named indexing
to display the gender and blood pressure of the patients Williams and Brown.

With cell arrays or structures, you do not have the same flexibility
to use named or numeric indexing.

With a cell array, you must use strcmp to
find desired named data, and then you can index into the array.

With a scalar structure or structure array, it is
not possible to refer to a field by number. Furthermore, with a scalar
structure, you cannot easily select a subset of variables or a subset
of observations. With a structure array, you can select a subset of
observations, but you cannot select a subset of variables.

With a table, you can access data by named index or
by numeric index. Furthermore, you can easily select a subset of variables
and a subset of rows.

Use Table Properties to Store Metadata

In addition to storing data, tables have properties to store
metadata, such as variable names, row names, descriptions, and variable
units. You can access a property using T.Properties.PropName,
where T is the name
of the table and PropName is
one of the table properties.

For example, add a table description, variable descriptions,
and variable units for Age.