Specify Types of Entry-Point Function Inputs
Because C and C++ are statically typed languages, the code generator must determine the class and size of all variables in the generated code during code generation. To generate C/C++ code with specific variable types, you must specify the types of all inputs to your entry-point function. Input-type specification is a critical step in the Code Generation Workflow.
If your entry-point functions do not have inputs, you can skip this step. You do not need to specify the class and size of inputs to non-entry-point functions.
Methods of Input-Type Specification
You must specify the class and size of
all inputs to your entry-point functions. For
aggregate types, such as classes, structures, and cell arrays, you must also specify the
class and size of each property, field, and element, respectively. You can specify input
types in the MATLAB®
Coder™ app, at the command line by using the codegen
command with the -args
option, in your
MATLAB entry-point function by using function argument validation (arguments
blocks), or in your MATLAB code by using preconditioning (assert
statements). This table outlines the advantages and disadvantages
of each of these methods.
Method | Advantages | Disadvantages |
---|---|---|
|
| |
|
| |
Use Function Argument Validation to Specify Entry-Point Input Types |
|
|
|
|
Unused Inputs
If one or more of your entry-point functions has unused input variables, code generation has certain considerations that depend on the method of input-type specification you use.
If you specify input types by using the
codegen
function with the-args
option, you do not have to specify the unused input variables. However, all used variables must appear before all unused variables in the function declaration. Unused and unspecified variables do not appear in the generated code.If all of the inputs to your MATLAB entry-point functions are unused and you specify the input types by using the
codegen
function with the-args
option, you can generate a C/C++ function with no inputs by passing an empty cell array{}
to-args
.If you use the MATLAB Coder app to specify input types, you must specify the types of the unused inputs. The unused inputs appear in the generated code.
If you define the input types in your MATLAB code using
arguments
blocks orassert
statements, you must specify the types of the unused inputs. You cannot specify unused inputs by using the tilde character (~
). The unused inputs appear in the generated code.
Tips
To specify an input that is a fixed-point
fi
(Fixed-Point Designer) object, you must configure the associatednumerictype
(Fixed-Point Designer) andfimath
(Fixed-Point Designer) objects.To generate a multisignature MEX function from an entry-point function, provide multiple
-args
specifications for the same entry-point function. The generated MEX function works with the multiple signatures that you provide during code generation. For more information on multisignature MEX function generation, see Generate Code for Functions with Multiple Signatures.If you generate a MEX, SIL, or PIL function that accepts an empty array with a given shape, the generated function accepts empty arrays of other shapes as well. The MEX, SIL, or PIL function reshapes the empty run-time input array to match the shape of the array that was specified during code generation. See Incompatibility with MATLAB Due to Resizing of Empty Arrays by MEX, SIL, and PIL Functions.