Contenido principal

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.

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

Define Tables for Code Generation

To define a table in MATLAB® code for code generation, use the table function. Alternatively, you can use the array2table, cell2table, and struct2table functions to convert arrays, cell arrays, and structures to tables. For code generation, you must supply table variable names when you create a table. Table variable names do not have to be valid MATLAB identifiers. Variable names can include any ASCII characters (such as commas, dashes, and space characters).

For example, suppose A, B, and C are 10-by-1 arrays of doubles, and vnames is a cell array of column names. You can generate code for a function that creates a table using these arrays as table variables.

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

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.

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:

See Also

Topics