Control Block Parameter Data Types
A block parameter, such as the Gain parameter of a
Gain block, has a data type in the same way that a signal has a data
type (see Control Data Types of Signals). MATLAB® variables, Simulink.Parameter
objects, and other
parameter objects that you use to set block parameter values also have data types.
Control block parameter data types to:
Accurately simulate the execution of your control algorithm on hardware.
Generate efficient code.
Integrate the generated code with your custom code.
Avoid using data types that your target hardware does not support.
Reduce Maintenance Effort with Data Type Inheritance
By default, block parameters, numeric MATLAB variables that use the data type double
, and
Simulink.Parameter
objects acquire a data type through
inherited and context-sensitive data typing. For example, if the input and output
signals of a Gain block use the data type single
,
the Gain parameter typically uses the same data type. If you
use a Simulink.Parameter
object to set the value of the block
parameter, by default, the object uses the same data type as the parameter. You can
take advantage of this inheritance to avoid explicitly specifying data types for
parameters.
Some inheritance rules choose a parameter data type other than the data type that the corresponding signals use. For example, suppose that:
The input and output signals of a Gain block use fixed-point data types with binary-point-only scaling.
On the Parameter Attributes tab, Parameter data type is set to
Inherit: Inherit via internal rule
(the default).On the Parameter Attributes tab, you specify a minimum and maximum value for the parameter by using Parameter minimum and Parameter maximum.
The data type setting Inherit: Inherit via internal
rule
can cause the block to choose a different data type, with a
different word length or scaling, than the data type that the signals use. The
minimum and maximum values that you specified for the parameter influence the
scaling that the block chooses.
When you select internal rules (Inherit: Inherit via internal
rule
) to enable Simulink® to choose data types, before simulating or generating code, configure
the characteristics of your target hardware. The internal rules can use these
settings to choose data types that yield efficient generated code.
Context-Sensitive Data Typing
When you use a MATLAB variable or Simulink.Parameter
object to set the
value of a block parameter, you can configure the variable or parameter object
to use context-sensitive data typing. When you simulate
or generate code, the variable or parameter object uses the same data type as
the block parameter. With this technique, you can match the data type of the
variable or parameter object with the data type of the block parameter. To
control the data type of the block parameter and the variable or object, you
specify only the data type of the block parameter.
To use context-sensitive data typing, set the value of a MATLAB variable to a double
value. For a
Simulink.Parameter
object, set the
Value
property by using a double
value and set the DataType
property to
auto
(the default).
Context-Sensitive Data Typing for Structure Fields
As described in Organize Related Block Parameter Definitions in Structures, you can organize multiple block parameter values into a structure.
The fields of parameter structures do not support context-sensitive data typing. However, to match the data type of a field with the data type of another data item in a model, you can use a bus object and a data type object.
Use a
Simulink.Bus
object as the data type of the structure.Use a
Simulink.AliasType
orSimulink.NumericType
object as the data type of the element in the bus object and as the data type of the target data item.
Techniques to Explicitly Specify Parameter Data Types
Many blocks supported for discrete-time simulation and code generation (such as those in the built-in Discrete library) enable you to explicitly specify parameter data types. For example, in an n-D Lookup Table block dialog box, on the Data Types tab, you can specify a data type for the lookup table data by using the Table data parameter. In a Gain block dialog box, use the Parameter Attributes tab to set Parameter data type, which controls the data type of the Gain parameter.
Some blocks, such as those in the Continuous library, do not enable you to specify
parameter data types. These block parameters use internal rules to choose a data
type. To indirectly control the data type of such a parameter, apply the data type
to a Simulink.Parameter
object instead.
When you use a Simulink.Parameter
object or other parameter
object to set the value of a block parameter, you can use the
DataType
property of the object to specify a data
type.
If you use model arguments, you can specify a data type:
For the model argument that you store in the model workspace.
With some blocks (such as those in the Discrete library), for the block parameter that uses the model argument.
For the argument value that you specify in a Model block.
The default settings for these data types typically use inheritance and
context-sensitive data typing. For example, the default value of the
DataType
property of a Simulink.Parameter
object is auto
, which causes the parameter object to acquire a
data type from the block parameter or parameters that use the object.
To explicitly specify a data type, you can use the Data Type Assistant in block dialog boxes and property dialog boxes. For information about the Data Type Assistant, see Specify Data Types Using Data Type Assistant.
Use the Model Data Editor for Batch Editing
Using the Model Data Editor (on the Modeling tab, click
Model Data Editor), you can specify the same data type for
multiple block parameters simultaneously. On the Parameters
tab, set the Change view drop-down list to
Design
and specify data types by using the
Data Type column.
For more information, see Model Data Editor.
Calculate Best-Precision Fixed-Point Scaling for Tunable Block Parameters
When you apply fixed-point data types to a model, you can use the Data Type
Assistant and the Fixed-Point Tool to calculate best-precision scaling for tunable
block parameters. A block parameter, Simulink.Parameter
object, or
other parameter object is tunable if it appears in the
generated code as a variable stored in memory.
The chosen scaling must accommodate the range of values that you expect to assign to the parameter. To enable the tools to calculate an appropriate scaling, specify the range information in the block or in a parameter object. Then, use one of these techniques to calculate the scaling:
Use the Fixed-Point Tool to autoscale the entire model or subsystem. The tool can propose and apply fixed-point data types for data items including block parameters,
Simulink.Parameter
objects, signals, and states.Configure individual block parameters or parameter objects to calculate their own scaling.
When you later change value ranges for parameters, this technique enables you or the model to recalculate the scaling without having to autoscale the entire model. However, if changing the value range of the parameter also changes the value range of an associated signal, you must manually calculate and apply a new scaling for the signal or use the Fixed-Point Tool to autoscale the model or subsystem.
For basic information about fixed-point data types, block parameters, and other tools and concepts, use the information in the table.
Topic | More Information |
---|---|
Fixed-point data types and scaling | Fixed-Point Numbers in Simulink (Fixed-Point Designer) |
How to specify value range information for block parameters and parameter objects | Specify Minimum and Maximum Values for Block Parameters |
How to use the Data Type Assistant | Specify Data Types Using Data Type Assistant |
Tunability and block parameter representation in the generated code | How Generated Code Stores Internal Signal, State, and Parameter Data (Simulink Coder) and Create Tunable Calibration Parameter in the Generated Code (Simulink Coder) |
Autoscale Entire Model by Using the Fixed-Point Tool
You can use the Fixed-Point Tool to autoscale data items in your model, including tunable parameters and signals whose values depend on those parameters. If you use this technique:
To configure parameters as tunable, use parameter objects (for example,
Simulink.Parameter
) instead of the Model Parameter Configuration dialog box. The Fixed-Point Tool can autoscale parameter objects, but cannot autoscale numeric variables that you select through the Model Parameter Configuration dialog box.If your model already uses the Model Parameter Configuration dialog box, use the
tunablevars2parameterobjects
function to create parameter objects instead.When you use
Simulink.Parameter
objects to set block parameter values, specify the value range information in the objects instead of the blocks. The Fixed-Point Tool uses the range information in each object to propose a data type for that object.To enable the tool to autoscale parameter values that you store as fields of a structure, use a
Simulink.Bus
object as the data type of the entire structure. Specify the range information for each field by using theMin
andMax
properties of the corresponding element in the bus object. The tool can then apply a data type to each element by using theDataType
property.To use a bus object as the data type of a parameter structure, see Control Field Data Types and Characteristics by Creating Parameter Object.
Before you apply the data types that the Fixed-Point Tool proposes, clear the proposals for parameters and parameter objects whose data types you do not want the tool to change. For example, clear the proposals for these entities:
Parameter objects that you import into the generated code from your own user-written code by applying a storage class such as
ImportedExtern
.Simulink.Parameter
model arguments in a model workspace.Alternatively, before autoscaling the model, consider replacing these parameter objects with numeric MATLAB variables to prevent the Fixed-Point Tool from autoscaling them.
Allowing the tool to autoscale model arguments can increase the risk of unintentional data type mismatches between the model argument values (which you specify in Model blocks in a parent model), the model arguments in the model workspace, and the client block parameters in the model.
Parameter objects whose
DataType
property is set toauto
(context-sensitive). Clear the proposals if you want the parameter objects to continue to use context-sensitive data typing.
For more information about using the Fixed-Point Tool to autoscale
Simulink.Parameter
objects, see Autoscaling Data Objects Using the Fixed-Point Tool (Fixed-Point Designer).
Calculate Best-Precision Scaling for Individual Parameters
You can configure a block parameter or Simulink.Parameter
object to calculate its own best-precision scaling. First, specify value range
information for the target parameter or parameter object. Then, use the Data
Type Assistant or the function fixdt
to apply a data type
to the parameter or object. Use these techniques when you do not want to use the
Fixed-Point Tool to autoscale the model.
Enable Block Parameter to Automatically Calculate Best-Precision Scaling. You can enable the parameters of some blocks (typically blocks in the Discrete library) to automatically calculate best-precision fixed-point scaling. Use this technique to store the range and data type information in the model instead of a parameter object. When you use this technique, if you later change the range information, the block parameter automatically recalculates best-precision scaling.
In the block dialog box, use the function fixdt
to
specify a fixed-point data type with unspecified scaling. For example, use
best-precision scaling for lookup table data, and store the data in a 16-bit
word:
On the Data Types tab of an n-D Lookup Table block, under the Minimum and Maximum columns, specify a value range for the elements of the table data.
Under the Data Type column, set the table data type to
fixdt(1,16)
.If you use a tunable
Simulink.Parameter
object to set the value of the table data parameter, set theDataType
property of the object toauto
. In the generated code, the parameter object uses the same scaling as the block parameter.
When you simulate or generate code, the lookup table data uses a signed 16-bit fixed-point data type whose binary-point scaling depends on the range information that you specify. The calculated scaling allows the fixed-point type to represent values that lie within the range. If you later change the minimum or maximum values, the block recalculates the scaling when you simulate or generate code.
Calculate Scaling for Parameter Object. If you use a Simulink.Parameter
object to set the values
of multiple block parameters, and if the block parameters use different data
types (including different fixed-point scaling), you cannot set the
DataType
property of the object to
auto
(the default). Instead, you can calculate
best-precision fixed-point scaling for the parameter object by specifying
range and data type information in the object. You can also use this
technique to store range and data type information in a parameter object
instead of a block dialog box. When you use this technique, if you later
change the range information, you must recalculate the best-precision
scaling by using the Data Type Assistant.
Suppose that you create a parameter object to represent the value
15.25
, and that the design range of the value is
between 0.00
and 32.00
. To calculate
best-precision scaling, use the Data Type Assistant.
At the command prompt, create a parameter object in the base workspace whose value is
15.25
.myParam = Simulink.Parameter(15.25);
In the MATLAB Workspace browser, double-click the object
myParam
. The property dialog box opens.Specify range information in the object. For example, set Minimum to
0.00
and Maximum to32.00
.Set Data type to
fixdt(0,16,0)
.Expand the Data Type Assistant and click Calculate Best-Precision Scaling.
The data type changes from
fixdt(0,16,0)
tofixdt(0,16,10)
.
The calculated scaling (a fraction length of 10 bits) enables the fixed-point data type to represent parameter values that lie within the range that you specified.
If you specify range and data type information in a parameter object, consider removing the range and data type information from the blocks that use the object. Some tools, such as the Fixed-Point Tool, ignore the range information that you specify in the block and use only the information in the parameter object. Removing the information from the block prevents confusion and user errors.
For example, on the Parameter Attributes tab of a
Gain block dialog box, set Parameter
minimum and Parameter maximum to
[]
. Set Parameter data type to
an inherited data type such as Inherit: Inherit from
'Gain'
so that the block parameter uses the same data type
as the parameter object.
Detect Numerical Accuracy Issues Due to Quantization and Overflow
When the data type of a block parameter, MATLAB variable, or parameter object cannot represent the value that you
specify, the data type quantizes the value, compromising numerical accuracy. For
example, the 32-bit floating-point data type single
(float
in C code) cannot exactly represent the parameter
value 1.73
. When the real-world value of a data item lies outside
the range of values that the data type can represent, overflow can cause loss of
information.
To detect these issues, use the diagnostic configuration parameters under Configuration Parameters > Diagnostics > Data Validity > Parameters. Set the values of these diagnostic configuration parameters to
warning
or error
:
Detect downcast
Detect precision loss
Detect underflow
Detect overflow
Reuse Custom C Data Types for Parameter Data
In a model, you can create parameter data that conform to custom C data types, such as structures, that your existing C code defines. Use these data to:
Replace existing C code with a Simulink model.
Integrate C code for simulation in Simulink (for example, by using the Legacy Code Tool).
Prepare to generate code (Simulink Coder™) that you can integrate with existing code.
Use these techniques to match your custom data types:
For a structure type, create a
Simulink.Bus
object. Use the object as the data type for a structure that you store in aSimulink.Parameter
object. See Organize Related Block Parameter Definitions in Structures.For an enumeration, create an enumeration class and use it as the data type for block parameters. See Use Enumerated Data in Simulink Models.
To match a
typedef
statement that represents an alias of a primitive, numeric data type, use aSimulink.AliasType
object as the data type for block parameters. SeeSimulink.AliasType
.
To create these classes and objects, you can use the function Simulink.importExternalCTypes
.
If a MATLAB Function block or Stateflow® chart in your model uses an imported enumeration or structure type,
configure the model configuration parameters to include
(#include
) the type definition from your external header file.
See Control Imported Bus and Enumeration Type Definitions (for a
MATLAB Function block) and Access Custom Code Variables and Functions in Stateflow Charts (Stateflow) and Integrate Custom Structures in Stateflow Charts (Stateflow) (for a chart).
Data Types of Mathematical Expressions
If you specify a block parameter using a mathematical expression, the block determines the final parameter data type using a combination of MATLAB and Simulink data typing rules.
Suppose that you define two parameter objects int8Param
and
doubleParam
, and use the objects to specify the
Constant value parameter in a Constant
block.
int8Param = Simulink.Parameter(3); int8Param.DataType = 'int8'; doubleParam = Simulink.Parameter(9.36); doubleParam.DataType = 'double';
The Constant block determines the data type of the Constant value parameter using these steps:
Each parameter object casts the specified numeric value to the specified data type.
Parameter object Data type Numeric value Result int8Param
int8
3
int8(3)
doubleParam
double
9.36
double(9.36)
The block evaluates the specified expression,
int8Param * doubleParam
, using MATLAB rules.An expression that involves a
double
data type and a different type returns a result of the different type. Therefore, the result of the expressionint8(3) * double(9.36)
isint8(28)
.
If you use an expression to set the value of a parameter object (such as
Simulink.Parameter
), parameter objects used in the expression
follow different data typing rules. The auto
setting of the
DataType
property has a slightly different meaning. See
Set Variable Value by Using a Mathematical Expression.
Block Parameter Data Types in the Generated Code
For more information about controlling parameter data types in the generated code, see Parameter Data Types in the Generated Code (Simulink Coder).