Define Table Inputs
You can define table inputs at the command line or in the MATLAB®
Coder™ app. Code generation does not support the programmatic specification of
table
input types by using function argument validation (arguments
blocks)
or by using preconditioning (assert
statements).
Define Table Inputs at the Command Line
Define table inputs at the command line by providing an example input or by using a
table type. You can also specify a constant table input.
Alternatively, if you have a test file that calls your entry-point function with example
inputs, you can determine the input types by using coder.getArgTypes
.
Provide an Example Table Input
Use the -args
option:
T = table(A,B,C,'VariableNames',vnames); codegen myFunction -args {T}
Provide a Table Type
To provide a type for a table to codegen
:
Define a table. For example:
T = table(A,B,C,'VariableNames',vnames);
Create a type from
T
.t = coder.typeof(T);
Pass the type to
codegen
by using the-args
option.codegen myFunction -args {t}
Provide a Constant Table Input
To specify that a table input is constant, use coder.Constant
with the -args
option:
T = table(A,B,C,'VariableNames',vnames); codegen myFunction -args {coder.Constant(T)}
Representation of Tables
A coder type object for a table describes the object and its properties. Create a new
table type by using coder.typeof
or coder.newtype
.
The coder type object displays a succinct description of the object properties while
excluding internal state values. The command line interface displays the type and size of
nonconstant properties and the values of constant properties. For example, create a coder
table
type with a size of 3-by-3.
A = [1 2 3]'; B = [4 5 6]'; C = [7 8 9]'; t = table(A,B,C); tType = coder.typeof(t)
The representation of variable t
is stored in coder type object
tType
.
tType = matlab.coder.type.TableType 3x3 table Data : 1x3 homogeneous cell Description : 1x0 char UserData : 0x0 double DimensionNames : {'Row'} {'Variables'} VariableNames : {'A'} {'B'} {'C'} VariableDescriptions : 1x3 homogeneous cell VariableUnits : 1x3 homogeneous cell VariableContinuity : 1x3 matlab.internal.coder.tabular.Continuity RowNames : 0x0 homogeneous cell
If necessary, you can obtain the legacy coder.ClassType
representation of a table
coder type by using the method
getCoderType
. For example, to view the underlying
coder.ClassType
representation of the tType
object,
use this
command:
tType.getCoderType
ans = coder.ClassType 1×1 table Properties : data : 1×0 homogeneous cell cell with no elements metaDim : 1×1 matlab.internal.coder.tabular.private.metaDim Properties : labels : {'Row'} {'Variables'} length : 1×1 double rowDim : 1×1 matlab.internal.coder.tabular.private.rowNamesDim Properties : labels : 0×0 homogeneous cell cell with no elements length : 0 varDim : 1×1 matlab.internal.coder.tabular.private.varNamesDim Properties : descrs : 1×0 homogeneous cell cell with no elements units : 1×0 homogeneous cell cell with no elements continuity : 0×0 double customProps : 1×1 struct with no fields hasDescrs : 1×1 logical hasUnits : 1×1 logical hasContinuity : 1×1 logical hasCustomProps : 1×1 logical labels : length : 1×1 double arrayProps : 1×1 struct Description: 1×0 char UserData: 0×0 double
Object Properties
You can edit the properties of coder table
type objects. You can
assign scalar values to object properties. Values are implicitly converted to the
corresponding coder type values when they are assigned to coder type object properties. You
can resize objects themselves by using the coder.resize
function or by editing object properties
directly.
Resize Object Properties by Using coder.resize
You can resize table
objects and object properties by using
coder.resize
. You can also create arrays within properties.
For example, create a coder table
type with a size of 3-by-3. The
Description
property has a size of
1-by-0.
A = [1 2 3]'; B = [4 5 6]'; C = [7 8 9]'; t = table(A,B,C); tType = coder.typeof(t)
tType = matlab.coder.type.TableType 3x3 table Data : 1x3 homogeneous cell Description : 1x0 char UserData : 0x0 double DimensionNames : {'Row'} {'Variables'} VariableNames : {'A'} {'B'} {'C'} VariableDescriptions : 1x3 homogeneous cell VariableUnits : 1x3 homogeneous cell VariableContinuity : 1x3 matlab.internal.coder.tabular.Continuity RowNames : 0x0 homogeneous cell
Use coder.resize
to make Description
variable
length with an upper bound of
12.
tType.Description = coder.resize(tType.Description,[1 12],[0 1])
tType = matlab.coder.type.TableType 3x3 table Data : 1x3 homogeneous cell Description : 1x:12 char UserData : 0x0 double DimensionNames : {'Row'} {'Variables'} VariableNames : {'A'} {'B'} {'C'} VariableDescriptions : 1x3 homogeneous cell VariableUnits : 1x3 homogeneous cell VariableContinuity : 1x3 matlab.internal.coder.tabular.Continuity RowNames : 0x0 homogeneous cell
Resize Objects Directly
You can also resize certain type objects themselves by editing the object properties.
For example, to change the number of rows in the tType
object, edit the
Size
property.
tType.Size = [10 3]
tType = matlab.coder.type.TableType 10x3 table Data : 1x3 homogeneous cell Description : 1x:12 char UserData : 0x0 double DimensionNames : {'Row'} {'Variables'} VariableNames : {'A'} {'B'} {'C'} VariableDescriptions : 1x3 homogeneous cell VariableUnits : 1x3 homogeneous cell VariableContinuity : 1x3 matlab.internal.coder.tabular.Continuity RowNames : 0x0 homogeneous cell
You can also make the number of rows variable size by using the
VarDims
property.
tType.VarDims = [true false]
tType = matlab.coder.type.TableType :10x3 table Data : 1x3 homogeneous cell Description : 1x:12 char UserData : 0x0 double DimensionNames : {'Row'} {'Variables'} VariableNames : {'A'} {'B'} {'C'} VariableDescriptions : 1x3 homogeneous cell VariableUnits : 1x3 homogeneous cell VariableContinuity : 1x3 matlab.internal.coder.tabular.Continuity RowNames : 0x0 homogeneous cell
See Also
table
| coder.Constant
| coder.typeof
| coder.newtype