Data Definition Considerations for Code Generation - MATLAB & Simulink (original) (raw)
To generate efficient standalone code, you must define the following types and classes of data differently from when you run your code in MATLABĀ®.
Data | Type Considerations | More Information |
---|---|---|
Arrays | Maximum number of elements is restricted. | Array Size Restrictions for Code Generation |
Numeric types | Assign numeric type variables a value before using them in operations or returning them as outputs. | Best Practices for Defining Variables for C/C++ Code Generation |
Complex numbers | Set complexity of variables at the time of assignment and before first use.Expressions containing a complex number or variable evaluate to a complex result, even if the imaginary part of the result is zero. | Code Generation for Complex Data |
Characters and strings | Characters are restricted to 8 bits of precision.For code generation, string scalars do not support global variables, indexing with curly braces, missing values, or size changes by using the functioncoder.varsize. | Encoding of Characters in Code GenerationCode Generation for Strings |
Variable-Size data | After initial fixed-size assignment to a variable, attempts to grow the variable might cause a compilation error. | Code Generation for Variable-Size ArraysDefine Variable-Size Data for Code Generation |
Structures | Assign fields to structures in the same order on each control path.Assign corresponding fields in the structure array elements with same size, type, and complexity. | Define Scalar Structures for Code GenerationDefine Arrays of Structures for Code Generation |
Cell arrays | Assign all cell array elements before passing the cell array to a function or returning it from a function.Variable-size cell array elements must all have the same size, type, and complexity. | Code Generation for Cell ArraysCell Array Limitations for Code Generation |
Tables | You must specify variable names by using the 'VariableNames' name-value argument when creating tables from input arrays.Limited data type support when you preallocate a table by using the table function and the 'Size' name-value argument.Table indices that specify variables must be compile time constant.You cannot change the size of a table by assignments.You cannot change the VariableNames,RowNames, DimensionNames, orUserData properties of a table after you create it.Limitations that apply to classes also apply to tables. | Code Generation for TablesTable Limitations for Code Generation |
Categorical arrays | Categorical arrays do not support these inputs and operations: Arrays of MATLAB objectsSparse matricesDuplicate category namesGrowth by assignmentAdding a categoryDeleting an elementLimitations that apply to classes also apply to categorical arrays. | Code Generation for Categorical ArraysCategorical Array Limitations for Code Generation |
Datetime arrays | datetime arrays do not support these inputs and operations: Text inputsThe 'Format' name-value argumentThe 'TimeZone' name-value argument and the 'TimeZone' propertySetting time component propertiesGrowth by assignmentDeleting an elementLimitations that apply to classes also apply todatetime arrays. | Code Generation for Datetime ArraysDatetime Array Limitations for Code Generation |
Duration arrays | Duration arrays do not support these inputs and operations: Text inputsGrowth by assignmentDeleting an elementConverting duration values to text by usingchar,cellstr, or string functionsLimitations that apply to classes also apply to duration arrays. | Code Generation for Duration ArraysDuration Array Limitations for Code Generation |
Timetables | You must specify variable names by using the 'VariableNames' name-value argument when creating timetables from input arrays.Limited data type support when you preallocate a table by using the timetable function and the 'Size' name-value argument.Timetable indices that specify variables must be compile time constant.You cannot change the size of a timetable by assignments.You cannot change theVariableNames,DimensionNames, orUserData properties of a timetable after you create it.If you create a regular timetable, and you attempt to set irregular row times, then an error is produced.If you create an irregular timetable, then it remains irregular even if you set its sample rate or time step.Limitations that apply to classes also apply to timetables. | Code Generation for TimetablesTimetable Limitations for Code Generation |
Enumerated data | Supports integer-based enumerated types only. | Enumerations |
MATLAB Classes | Before generating code, it is a best practice to test class property validation by running a MEX function over the full range of input values.If a property does not have an explicit initial value, the code generator assumes that it is undefined at the beginning of the constructor. The code generator does not assign an empty matrix as the default.The coder.varsize function is not supported for class properties.If the initial value of a property is an object, then the property must be constant. To make a property constant, declare the Constant attribute in the property block. | Generate C++ Classes for MATLAB ClassesMATLAB Classes Definition for Code Generation |
Function handles | Assigning different function handles to the same variable can cause a compile-time error.You cannot pass function handles to or from entry-point functions or extrinsic functions.You cannot view function handles from the MATLAB Function Block debugger. | Function Handles |
Dictionaries | You cannot create a dictionary by passing an array of strings to the dictionary function.Cell arrays used as keys or values must contain the same pattern of data types.Structures used as keys or values must have the same fields with the same data types.If you use structures or cell arrays as keys or values, the code generator determines whether cell array elements and structure fields vary in size when the dictionary is created.You cannot index into a dictionary using curly braces{}.You cannot use objects as keys.You cannot use function handles as keys or values. | Generate Code for DictionariesDictionary Limitations for Code Generation |
Deep learning arrays | dlarrays do not support these inputs and operations: The data format argument must be a compile-time constantDefine dlarray variables inside the entry-point function.The input to a dlarray must be fixed-size.Code generation does not support creating adlarray type object by using the coder.typeof function with upper bound size and variable dimensions specified. | Code Generation for dlarraydlarray Limitations for Code Generation |
The information in the preceding table is not an exhaustive list of considerations for each data type. See the topics in the More Information column.