Kahibaro
Discord Login Register

Creating Tables and Accessing Data

Why Use Tables in MATLAB

Tables in MATLAB let you store data with different types in one container, organized by named variables and row labels. They are especially useful for data that looks like a spreadsheet, where each column has a meaning, such as Age, Height, or Name, and each row corresponds to an observation.

Unlike plain numeric arrays, tables keep track of column names and can also hold metadata. This makes your code easier to read and maintain, especially when you work with real-world data.

Creating Tables from Workspace Variables

The most direct way to create a table is from several existing workspace variables. Each input becomes one variable (column) of the table.

Suppose you have three vectors in the workspace.

Name   = ["Alice"; "Bob"; "Charlie"];
Age    = [25; 32; 29];
Height = [1.65; 1.80; 1.75];

You can create a table with:

T = table(Name, Age, Height);

MATLAB automatically uses the workspace variable names as table variable names. You can check the result by typing the table’s name in the Command Window:

T

By default, row names are not assigned. Row indices are simple numerical indices just like in arrays.

You can also define custom variable names when creating the table. This is useful if the variable names in the workspace are long or not ideal as column labels.

T = table(Name, Age, Height, ...
          'VariableNames', {'PersonName', 'Years', 'Meters'});

Now the table has variables PersonName, Years, and Meters even though your original workspace variables have the names Name, Age, and Height.

Creating Tables from Different Data Types

One strength of tables is that each column can have a different data type. You can mix numeric arrays, string arrays, categorical arrays, and logical values in the same table, as long as each column itself is a valid array.

For example:

Name       = ["Alice"; "Bob"; "Charlie"];
Age        = [25; 32; 29];
IsStudent  = [true; false; true];
City       = categorical(["London"; "Madrid"; "London"]);
T = table(Name, Age, IsStudent, City);

Here, Name is a string array, Age is numeric, IsStudent is logical, and City is categorical. MATLAB preserves all these types in the table.

If you want to include a column that is not a simple vector, such as a different size for each row, you often need a cell array. For example, if you store comments of variable length:

Comments = {
    "Excellent performance"
    "Average"
    "Needs improvement"
};
T = table(Name, Age, Comments);

Each entry in Comments can have a different length, and the column is treated as a cell array inside the table.

Specifying Row Names

Tables can have row names, which act like labels for rows. They are useful when your rows represent named entities, such as patient IDs, experiment IDs, or dates.

You can set row names when creating the table:

PatientID = ["P001"; "P002"; "P003"];
Age       = [45; 52; 36];
Weight    = [70.5; 82.3; 65.0];
T = table(Age, Weight, 'RowNames', cellstr(PatientID));

Here, Age and Weight are the table variables. The RowNames property uses a cell array of character vectors, so cellstr is applied to convert the string array to a cell array of character vectors.

Alternatively, you can assign row names after the table exists, by using the table property RowNames:

T = table(Age, Weight);
T.Properties.RowNames = {'P001', 'P002', 'P003'};

Row names must be unique and nonempty. They provide a label-based way to index rows, which you will see when accessing data.

Basic Inspection of Table Structure

Before accessing the data, you should know how to quickly inspect what a table contains. The simplest way is to display it in the Command Window:

T

For a more compact view, use summary:

summary(T)

The summary shows each variable’s type and basic statistics or information. This is very helpful when you work with larger or imported tables.

The size function works with tables too:

[nRows, nVars] = size(T);

You can access the variable names and row names through properties:

T.Properties.VariableNames
T.Properties.RowNames

These help you recall or check the labels you will use for indexing.

Accessing Entire Variables (Columns)

Columns in a table are called variables. You can access them in two main ways: dot indexing and curly or parentheses indexing. Dot indexing is the most natural when you want the full column by its name.

If you have:

T = table(Name, Age, Height);

You can extract the Age column as:

ages = T.Age;

Here, ages is the underlying array stored in that column, in this case a numeric column vector.

Dot indexing is usually the clearest and most readable way to access a full column. It also allows you to assign into table variables:

T.BMI = T.Age ./ (T.Height.^2);

This adds a new column BMI to the table.

You can also use parentheses with variable indices:

subT = T(:, {'Name', 'Age'});

In this case, subT is still a table, but with only the selected columns. Unlike dot indexing, parentheses give you a table subset, not the raw array.

Accessing Rows and Subtables

Rows in a table represent observations. You can access them by numeric indices or by row names if they exist.

To get the second row as a table:

row2 = T(2, :);

You use : for all columns, and 2 for the row index. The result is still a table, but with one row.

If your table has row names, for example T.Properties.RowNames = {'P001','P002','P003'};, you can select by row name:

r = T("P002", :);

You can also select multiple rows:

r = T(1:3, :);
r = T({"P001", "P003"}, :);

In each case, the result is a subtable that preserves the table structure and metadata.

Accessing Individual Cells and Values

Sometimes you do not want a subtable, but the actual values in a particular cell or block of cells. For example, you might need the numeric value in row 3 of the Age column.

To extract the raw array data, use curly braces with row and variable indexing:

age3 = T{3, 'Age'};

Now age3 is a numeric scalar, not a table. Curly braces always return the contents of the table at that location.

You can also extract a block of data:

M = T{1:3, {'Age', 'Height'}};

Here, M is a numeric array with 3 rows and 2 columns, containing just the values from the selected columns and rows.

Curly braces can also be used with variable indices instead of names:

val = T{2, 1};         % second row, first column contents
block = T{:, 2:3};     % all rows, columns 2 and 3 contents

Use parentheses when you want a subtable, and curly braces when you want the underlying array values.

Using Dot Indexing with Row Selection

Dot indexing can be combined with normal array indexing if you are already working with a specific variable. For example, you might have:

ages = T.Age;
thirdAge = ages(3);

But you can also write this in one step:

thirdAge = T.Age(3);

This accesses the Age column using dot indexing, then indexes the third element of that vector.

Similarly, for assigning values in a specific row of one column:

T.Age(2) = 33;

This changes the second row of the Age variable. Dot indexing followed by parentheses is a common pattern when you want to work inside a single column.

Selecting Multiple Columns by Name or Index

You often want only a subset of columns from a table. When you want to keep the result as a table, use parentheses with a list of variable names or variable indices.

Using names:

Views: 3

Comments

Please login to add a comment.

Don't have an account? Register now!