Define Input Parameter by Example by Using the App
Define an Input Parameter by Example
On the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of the input parameter that you want to define.
Select Define by Example.
In the field to the right of the parameter, enter a MATLAB® expression. The variable has the class, size, and complexity of the value of the expression.
Alternatively, you can select a variable from the list of workspace variables that displays.
Specify Input Parameters by Example
This example shows how to specify a 1-by-4
vector of unsigned
16-bit integers.
On the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of the input parameter that you want to define.
Select Define by Example.
In the field to the right of the parameter, enter:
zeros(1,4,'uint16')
The input type is
uint16(1x4)
.Optionally, after you specify the input type, you can specify that the input is variable size. For example, select the second dimension.
To specify that the second dimension is variable size with an upper bound of
4
, select:4
. Alternatively, to specify that the second dimension is unbounded, select:Inf
.
Alternatively, you can specify that the input is variable size by using
the coder.newtype
function. Enter the
MATLAB expression:
coder.newtype('uint16',[1 4],[0 1])
Note
To specify that an input is a double-precision scalar, enter
0
.
Specify a String Scalar Input Parameter by Example
This example shows how to specify a string scalar type by providing an example string.
On the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of the input parameter that you want to define.
Select Define by Example.
In the field to the right of the parameter, enter:
"mystring"
The input parameter is a 1-by-1 string array (string scalar) that contains a 1-by-8 character vector.
To make the string variable-size, click the second dimension.
To specify that the second dimension is unbounded, select
:Inf
.To specify that the second dimension has an upper bound, enter the upper bound, for example
8
. Then, select:8
.
Specify a Structure Type Input Parameter by Example
This example shows how to specify a structure with two fields, a
and b
. The input type of a
is scalar double. The
input type of b
is scalar char.
On the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of the input parameter that you want to define.
Select Define by Example.
In the field to the right of the parameter, enter:
struct('a', 1, 'b', 'x')
The type of the input parameter is
struct(1x1)
. The type of fielda
isdouble(1x1)
. The type of fieldb
ischar(1x1)
For an array of structures, to specify the size of each dimension, click the dimension and specify the size. For example, enter 4 for the first dimension.
To specify that the second dimension is variable size with an upper bound of
4
, select:4
. Alternatively, to specify that the second dimension is unbounded select:Inf
.
Alternatively, specify the size of the array of structures in the
struct
function call. For example, struct('a', { 1 2},
'b', {'x', 'y'})
specifies a 1x2 array of structures with fields
a
and b
. The type of field
a
is double(1x1)
. The type of field
b
is char(1x1)
.
To modify the type definition, see Specify a Structure Input Parameter.
Specify a Cell Array Type Input Parameter by Example
This example shows how to specify a cell array input by example. When you define a cell array by example, the app determines whether the cell array is homogeneous or heterogeneous. See Code Generation for Cell Arrays. If you want to control whether the cell array is homogeneous or heterogeneous, specify the cell array by type. See Specify a Cell Array Input Parameter.
On the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of the input parameter that you want to define.
Select Define by Example.
In the field to the right of the parameter, enter an example cell array.
If all cell array elements have the same properties, the cell array is homogeneous. For example, enter:
The input is a 1x3 cell array. The type of each element is{1 2 3}
double(1x1)
.The colon inside curly braces
{:}
indicates that all elements have the same properties.If elements of the cell array have different classes, the cell array is heterogeneous. For example, enter:
The input is a 1x2 cell array. For a heterogeneous cell array, the app lists each element. The type of the first element is{'a', 1}
char(1x1)
. The type of the second element isdouble(1x1)
.For some example cell arrays. the classification as homogeneous or heterogeneous is ambiguous. For these cell arrays, the app uses heuristics to determine whether the cell array is homogeneous or heterogeneous. For example, for the example cell array, enter:
The elements have the same class, but different sizes. The app determines that the input is a 1x2 heterogeneous cell array. The type of the first element is{1 [2 3]}
double(1x1)
. The type of the second element isdouble(1x2)
.However, the example cell array,
{1 [2 3]}
, can also be a homogeneous cell array whose elements are 1x:2 double. If you want this cell array to be homogeneous, do one of the following:Specify the cell array input by type. Specify that the input is a homogeneous cell array. Specify that the elements are 1x:2 double. See Specify a Cell Array Input Parameter.
Right-click the variable. Select Homogeneous. Specify that the elements are 1x:2 double.
If you use
coder.typeof
to specify that the example cell array is variable size, the app makes the cell array homogeneous. For example, for the example input, enter:The app determines that the input is a 1x:3 homogeneous cell array whose elements are 1x:2 double.coder.typeof({1 [2 3]}, [1 3], [0 1])
To modify the type definition, see Specify a Cell Array Input Parameter.
Specify an Enumerated Type Input Parameter by Example
This example shows how to specify that an input uses the enumerated type
MyColors
.
Suppose that MyColors.m
is on the MATLAB
path.
classdef MyColors < int32 enumeration green(1), red(2), end end
To specify that an input has the enumerated type MyColors
:
On the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of the input parameter that you want to define.
Select Define by Example.
In the field to the right of the parameter, enter the MATLAB expression:
MyColors.red
Specify an Object Input Type Parameter by Example
This example shows how to specify the type for an object of a value class myRectangle
.
classdef myRectangle properties length; width; end methods function obj = myRectangle(l,w) if nargin > 0 obj.length = l; obj.width = w; end end function area = calcarea(obj) area = obj.length * obj.width; end end end
Define a function that takes an object of the value class as an input. For example:
function z = getarea(r) %#codegen z = calcarea(r); end
In MATLAB, define an object
rect_obj
.rect_obj = myRectangle(3,4)
In the app, on the Select Source Files page, enter
getarea
for the entry-point function.On the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of
r
.Select Define by Example.
In the field to the right of
r
, enterrect_obj
or select it from the list of workspace variables. The app determines thatr
is a class with propertieslength
andwidth
.
Alternatively, you can provide a coder.ClassType
object for that class. To define a coder.ClassType
object, use coder.typeof
. For example:
In MATLAB, define a
coder.ClassType
object that has the same properties asrect_obj
.t = coder.typeof(rect_obj)
In the app, provide
t
as the example.
To change the size or type of a property, click the field to the right of the property.
When you generate code, the properties that you define in the app must be consistent with the properties in the class definition file. If the class definition file has properties that your code does not use, your type definition in the app does not have to include those properties. The code generator removes properties that your code does not use.
Specify a Fixed-Point Input Parameter by Example
To specify fixed-point inputs, Fixed-Point Designer™ software must be installed.
This example shows how to specify a signed fixed-point type with a word length of eight bits, and a fraction length of three bits.
On the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of the input parameter that you want to define.
Select Define by Example.
In the field to the right of the parameter, enter:
fi(10, 1, 8, 3)
The app sets the type of input
u
tofi(1x1)
. By default, if you do not specify a localfimath
, the app uses the defaultfimath
. See fimath for Sharing Arithmetic Rules (Fixed-Point Designer).Optionally, modify the fixed-point properties or the size of the input. See Specify a Fixed-Point Input Parameter and Specify Types of Entry-Point Inputs Using the App.
Specify an Input from an Entry-Point Function Output Type
When generating code for multiple entry-point functions, you can use the output type from one entry-point function as the input type to another entry-point function. For more information, see Pass an Entry-Point Function Output as an Input.
On the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of the input parameter that you want to define and select Use Output.
Select the name of the entry-point function and the corresponding output parameter from which to define the input type.
The MATLAB Coder™ app is not supported in MATLAB Online™.