Code Generation for Tables

The table data type is a data type suitable for column-oriented or tabular data that is often stored as columns in a text file or in a spreadsheet. Tables consist of rows and column-oriented variables. Each variable in a table can have a different data type and a different size with one restriction: each variable must have the same number of rows. For more information, see Tables (MATLAB).

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

Define Tables for Code Generation

For code generation, use the table function. For example, suppose the input arguments to your MATLAB® function are three arrays that have the same number of rows and a cell array that has variable names. You can create a table that contains these arrays as table variables.

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

You can use the array2table, cell2table, and struct2table functions to convert arrays, cell arrays, and structures to tables. For example, you can convert an input cell array to a table.

function T = foo(C,vnames) %#codegen
    T = cell2table(C,'VariableNames',vnames);
end

For code generation, you must supply table variable names when you create a table.

Table variables must have one of these data types:

  • numeric

  • logical

  • duration

  • categorical

  • char (cell arrays of character vectors)

Allowed Operations on Tables

For code generation, you are restricted to the operations on tables listed below.

OperationExampleNotes

assignment operator: =

T = table(A,B,C,'VariableNames',vnames);
T{:,1} = D;

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

  • Delete a variable or a row.

  • Add a variable or a row.

indexing operation

T = table(A,B,C,'VariableNames',vnames);
T(1:5,1:3);

Code generation supports indexing by position, variable or row name, and logical indexing.

To index by using variable or row names, first make input tables constant by using the coder.Constant function.

Code generation supports:

  • Table indexing with smooth parentheses, ().

  • Content indexing with curly braces, {}.

  • Dot notation to access a table variable.

concatenation

T1 = table(A,B,C,'VariableNames',vnames);
T2 = table(D,E,F,'VariableNames',vnames);
T = [T1 ; T2];

Code generation supports table concatenation.

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

  • For horizontal concatenation, tables must have the same number of rows. If the tables have row names, then they must have the same row names in the same order.

MATLAB Toolbox Functions That Support Tables

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

Related Topics