Define Types of Entry-Point Inputs by Using the MATLAB Coder App
You can use the MATLAB®
Coder™ app to define the types of input arguments to MATLAB entry-point functions. You can also use the app to view input type
definitions, set size constraints, control the number of output arguments, or define
constant input variables. Alternatively, you can specify input types by using the codegen
command with -args
at the command line or in your
MATLAB code by using arguments
blocks or assert
statements. For an overview of these
methods of input type specification, see Specify Types of Entry-Point Function Inputs.
Define Input Types
To define input types in the MATLAB Coder app, add one or more entry points to your MATLAB Coder project. After you add the entry points, the app highlights the entry-point inputs in red to indicate that input types are undefined.
To view details of the entry-point function inputs, expand the Entry Points pane by clicking the arrow to the left of the function signature . In the expanded view, the app displays an error if the input types are not fully defined.
For example, this image shows the signature of entry-point function
foo
, which has three undefined input arguments
a
, b
, and c
, and three
output arguments, x
, y
, and
z
.
Automatically Define Input Types
To define the input types automatically, use one of these methods:
Use a test bench. If you write a script that exercises your entry-point functions (also known as a test bench), you can automatically define the input types by using the script. Set the Automatically Define Input Types parameter to
Using Script
and enter the name of your test bench. The app automatically defines the input types.Call your entry-point functions at the command line. Set the Automatically Define Input Types parameter to
Using Command Window
. Click Start and call your entry-point functions at the command line.
Manually Define Input Types
To define input types manually, use one of these methods:
Define the variable class, size, and other properties manually. Click Click to define data type for each input variable in the Entry Points pane. Click the variable class definition and then select or enter the name of the class. To edit the size of a variable, click each dimension and enter the size. In the Size section of the tooltip, you can define each dimension as fixed size, variable size, or unbounded. For most data types, you can set additional properties by pointing to the column to the right of the size and clicking Edit Type Properties. For aggregate types, such as structures, user-written classes, and cell arrays, you must also define the types of all fields, properties, and elements.
Use example values to define the input types. Point to the right of a variable size and then click the Modes and Actions button . In the Actions section, click Derive type from example code and enter MATLAB code that returns an example of the desired type. Alternatively, click the variable class definition and select Derive type from example value. Then, enter MATLAB code that returns an example of the desired type.
Use Function Argument Validation or Preconditioning to Define Input Types
If an entry-point function uses an arguments
block or
assert
statements to define input types, the app uses these
definitions by default. To override these definitions, clear the Source
input types from function definition check box. For example, this
image shows that the app is using an arguments
block or
assert
statements in the entry-point function
bar
to define input argument d
as a
10-by-10 array of doubles.
Define Types for Multiple Input Variables
You can instruct the app to define undefined input variables as either scalar doubles or scalar singles for all entry-point functions or for specific entry-point functions.
To perform this assignment for undefined input variables in all entry-point functions, click the Actions button on the right side of the Entry Points pane. Click Define undefined types as and then Scalar double or Scalar single.
To perform this assignment for specific entry-point functions, click the Actions button to the right of the entry-point function signature. Click Define undefined types as and then Scalar double or Scalar single.
View Input Type Definitions
You can view the entry-point input variable definitions by expanding the Entry Points pane. The app displays the names of the entry-point input variables, and if the variable types are defined, the variable class, size, and other details. For composite types such as structures, cell arrays, or classes, you can expand the display of each variable to view the name, class, and other details of the individual fields, elements, or properties.
For example, this image shows the Entry Points pane for
entry-point function foo
. Function foo
has three
input arguments and returns a single output.
Input argument
a
is a fixed-size 1-by-1 structure array. The structure has three fields. Fieldf1
is an unbounded string, fieldf2
is a variable-length 1-by-12 character vector, and fieldf3
is a fixed-size scalar double.Input argument
b
is variable-size 10-by-5 array of doubles.Input argument
c
is a homogenous 2-by-3 cell array. Each element of the cell array contains a scalar double.
This table defines some of the symbols that appear in the Entry Points pane.
Indicator | Description |
---|---|
The variable has fields, elements, or properties that you can see by clicking the arrow. | |
{:} | Homogeneous cell array (all elements have the same properties). |
{n,m} | Element {n,m} of a heterogeneous cell
array. |
:n | Variable-size dimension with an upper bound of
n . |
:Inf | Variable-size dimension that is unbounded. |
Set Size Constraints
You can make the dimensions of the entry-point input arguments in your MATLAB Coder project variable size or unbounded when they meet certain size thresholds. By default, these constraints apply only to variables that you define manually, but you can choose to apply these constraints to input variables you define automatically.
To set variable sizing constraints, click the Actions button on the right side of the Entry Points pane and select Constrain Size. In the Set Size Constrains for Input Types dialog box, select the constraints.
To make a dimension variable-size with an upper bound, select the Make dimension variable-size if the size is at least check box, and then specify the threshold. If the size of a dimension of an input type is equal to or greater than this threshold, the app makes the dimension variable size, with an upper bound equal to the size of the dimension.
To make a dimension unbounded, select the Make dimension unbounded if the size is at least check box, and then specify the threshold. If the size of a dimension of an input is equal to or greater than this threshold, the app makes this dimension unbounded.
To apply the selected constraints to both automatically and manually defined input types, select the Apply size constrains when automatically defining input types check box.
To apply the rules to the current type definitions, click Ok. If you change type definitions manually, the rules do not affect the new definitions unless you reapply them.
Define Constant Input Variable
You can define an input variable as a constant. You can generate a constant value by using MATLAB code or by loading the constant from an existing MAT file. Point to a variable in the Entry Points pane, then click the Modes and Actions button . In the Modes section, select Use Constant.
To generate a value using a MATLAB command, select Use MATLAB code. Enter the MATLAB command that produces the desired value.
To load a value from a MAT file, select Load from MAT-file. Select or type the MAT file name and select the name of the variable whose value you want to use.
The code generator evaluates constant values and defines constant input variables at code generation time. Therefore, if you change the value of the source variable in the MAT file or if the result of the MATLAB command changes after code generation, the value of the constant variable does not update in the generated code.
For example, this image shows the Entry Points pane for function
foo
, which has three constant input arguments:
Input argument
a
has the value of the MATLAB command5
.Input argument
b
has the value of variablemyVar
from the filemyWorkspace.mat
.Input argument
c
has the value of the MATLAB commandbar(f)*g
.
Use an Entry-Point Function Output Type as an Entry-Point Function Input Type
If you generate code for multiple entry-point functions, you can use the output of one entry-point function as the input to another entry-point function.
Click the Modes and Actions button to the right of the variable that you want to define using the output of another entry-point function. In the Modes section, select Use Output. Enter the name of the function that produces the input value in the Function Name box and enter the name of the output variable in the Output Name box. Alternatively, expand the Function Name and Output Name boxes and select the function and output variable.
The app populates the class definition with the call to coder.OutputType
that produces the type of the input value. See Pass an Entry-Point Function Output as an Input.
For example, this image shows the Entry Points pane for
entry-point functions foo
and bar
. The input
argument a
to function foo
has the same type as
the first output of function bar
.
See Also
MATLAB Coder | arguments
| assert
| codegen
| coder.OutputType