Main Content

Code Generation for Timetables

The timetable data type is a data type suitable for tabular data with time-stamped rows. Like tables, timetables consist of rows and column-oriented variables. Each variable in a timetable can have a different data type and a different size with one restriction: each variable must have the same number of rows.

The row times of a timetable are time values that label the rows. You can index into a timetable by row time and variable. To index into a timetable, use smooth parentheses () to return a subtable or curly braces {} to extract the contents. You can refer to variables and to the vector of row times by their names. For more information, see Timetables.

When you use timetables with code generation, adhere to these restrictions.

Define Timetables for Code Generation

For code generation, use the timetable function. For example, suppose the input arguments to your MATLAB® function are three arrays that have the same number of rows (A, B, and C), a datetime or duration vector containing row times (D), and a cell array that has variable names (vnames). You can create a timetable that contains these arrays as timetable variables.

function TT = foo(A,B,C,D,vnames) %#codegen
    TT = table(A,B,C,'RowTimes',D,'VariableNames',vnames);
end

To convert arrays and tables to timetables, use the array2timetable and table2timetable functions. For example, you can convert an input M-by-N matrix to a timetable, where each column of the matrix becomes a variable in the timetable. Assign row times by using a duration vector.

function TT = foo(A,D,vnames) %#codegen
    TT = array2timetable(A,'RowTimes',D,'VariableNames',vnames);
end

For code generation, you must supply timetable variable names when you create a timetable. Timetable variable names do not have to be valid MATLAB identifiers. The names must be composed of ASCII characters, but can include any ASCII characters (such as commas, dashes, and space characters).

The row times can have either the datetime or duration data type.

Allowed Operations on Timetables

For code generation, you are restricted to the operations on timetables listed in this table.

OperationExampleNotes

Assignment operator: =

TT = timetable(A,B,C,'RowTimes',D,'VariableNames',vnames);
TT{:,1} = X;

Code generation does not support using the assignment operator = to:

  • Delete a variable or a row.

  • Add a variable or a row.

Indexing operation

D = seconds(1:10);
TT = timetable(A,B,C,'RowTimes',D,'VariableNames',vnames);
TT(seconds(3:7),1:3);

Code generation supports indexing by position, variable or row time, and logical indexing. Also, you can index using objects created by using the timerange or withtol functions.

Code generation supports:

  • Timetable indexing with smooth parentheses, ().

  • Content indexing with curly braces, {}.

  • Dot notation to access a timetable variable.

Concatenation

TT1 = timetable(A,B,C,'RowTimes',D1,'VariableNames',vnames);
TT2 = timetable(D,E,F,'RowTimes',D2,'VariableNames',vnames);
TT = [TT1 ; TT2];

Code generation supports timetable concatenation.

  • For vertical concatenation, timetables must have variables that have the same names in the same order.

  • For horizontal concatenation, timetables must have the same number of rows. They also must have the same row times in the same order.

MATLAB Toolbox Functions That Support Timetables

For code generation, you can use timetables with these MATLAB toolbox functions:

Related Topics