Parameter Data Types in the Generated Code

The data type of a block parameter (such as the Gain parameter of a Gain block), numeric MATLAB® variable, or Simulink.Parameter object determines the data type that the corresponding entity in the generated code uses (for example, a global variable or an argument of a function). To generate more efficient code, you can match parameter data types with signal data types or store parameters in smaller data types.

For basic information about setting block parameter data types in a model, see Control Block Parameter Data Types (Simulink).

Significance of Parameter Data Types

The data type that a block parameter, MATLAB variable, or parameter object uses determines the data type that the generated code uses to store the parameter value in memory. For example:

  • If you set the model configuration parameter Default parameter behavior (see Default parameter behavior) to Tunable, the Gain parameter of a Gain block appears in the generated code as a field of a global structure that stores parameter data. If you apply the data type single to the block parameter in the model, the structure field in the code uses the corresponding data type, real32_T.

  • If you apply the storage class ExportedGlobal to a Simulink.Parameter object, the object appears in the generated code as a separate global variable. If you set the DataType property of the object to int8, the global variable in the code uses the corresponding data type, int8_T.

  • If you configure a Simulink.Parameter object in a model workspace as a model argument, the object appears in the generated code as a formal parameter (argument) of a model entry-point function, such as the step function. The DataType property of the object determines the data type of the formal parameter.

Other than determining the data type that the generated code uses to store parameter values in memory, the data type of a parameter, variable, or object can also:

  • Cause the block to cast the value of the parameter prior to code generation. The cast can result in overflow, underflow, or quantization.

  • Cause the generated code to include extra code, for example saturation code.

Typecasts Due to Parameter Data Type Mismatches

When the data types of block parameters, workspace variables, and signals differ, blocks can use typecasts to reconcile the data type mismatches. These typecasts can cause the generated code algorithm, including the model step function, to include explicit casts to reconcile mismatches in storage data type and C bit shifts to reconcile mismatches in fixed-point scaling.

Parameter data type mismatches can occur when:

  • The data type that you specify for a MATLAB variable or parameter object (Simulink.Parameter) differs from the data type of a block parameter. The block parameter typecasts the value of the variable or object.

  • The data type that you specify for an initial value differs from the data type of the initialized signal or state.

  • The data type that you specify for a block parameter differs from the data type of the signal or signals that the parameter operates on. Some blocks typecast the parameter to perform the operation. For example, the Gain block performs this typecast.

If you configure a variable or object to use bias or fractional fixed-point slope, the block parameter cannot perform the typecast. In this case, you must match the data type of the variable or parameter object with the data type of the block parameter. Use one of these techniques:

  • Use context-sensitive data typing for the variable or parameter object. For a MATLAB variable, use a double number to set the value of the variable. For a parameter object, set the DataType property to auto.

  • Use a Simulink.AliasType or Simulink.NumericType object to set the data type of the block parameter and the data type of a parameter object.

    Use this technique when you cannot rely on context-sensitive data typing, for example, when you use the field of a structure to set the value of the block parameter.

  • Manually specify the same data type for the block parameter and the variable or parameter object.

    Use this technique to reduce the dependence of the model on inherited and context-sensitive data types and on external variables and objects.

For blocks that access parameter data through pointer or reference in the generated code, if you specify different data types for the workspace variable and the block parameter, the generated code implicitly casts the data type of the variable to the data type of the block parameter. Note, that an implicit cast requires a data copy which could significantly increase RAM consumption and slow down code execution speed for large data sets. For example, Lookup Table blocks often access large vectors or matrices through pointer or reference in the generated code.

For information about matching parameter data types when you use model arguments, see Control Data Types of Model Arguments and Argument Values.

Detect Downcast and Loss of Precision due to Data Type Mismatches

You can configure diagnostic configuration parameters to detect unintentional data type mismatches that result in quantization and loss of parameter precision. See Model Configuration Parameters: Data Validity Diagnostics (Simulink).

Considerations for Other Modeling Patterns

When you use specific modeling patterns and constructs such as fixed-point data types, parameter structures, and lookup table objects, use different techniques to control parameter data types.

Tunable Parameters and Best-Precision Fixed-Point Scaling

To apply best-precision fixed-point scaling to a tunable block parameter or parameter object, you can use the Fixed-Point Tool to autoscale an entire system or use the Data Type Assistant to configure individual parameters or objects. See Calculate Best-Precision Fixed-Point Scaling for Tunable Block Parameters (Simulink).

If a tunable parameter uses best-precision fixed-point scaling, Simulink® chooses a data type based on the minimum and maximum values that you specify for the parameter (see Specify Minimum and Maximum Values for Block Parameters (Simulink)). You can specify these values in the block dialog box that uses the parameter or in the properties of a Simulink.Parameter object.

If you do not specify a minimum or maximum, Simulink chooses a data type based on the value of the parameter. The chosen scaling might restrict the range of possible tuning values. Therefore, it is a best practice to specify minimum and maximum values for each tunable parameter.

A tunable parameter can use best-precision scaling even if you do not specify it in the parameter data type. For example, the Gain block can choose a best-precision scaling if the Parameter data type in the block dialog box is set to Inherit: Inherit via internal rule. This setting is the default for the block.

Control Data Types of Structure Fields

When you use a structure as the value of a block parameter (for example to initialize a bus signal), or when you organize multiple block parameter values into a single structure, you can create a Simulink.Bus object to use as the data type of a Simulink.Parameter object. You can then control the data types of individual fields in the structure. See Control Field Data Types and Characteristics by Creating Parameter Object (Simulink) and Control Data Types of Initial Condition Structure Fields (Simulink).

Control Data Types of Lookup Table Objects

When you use Simulink.LookupTable and Simulink.Breakpoint objects to store table and breakpoint data for a lookup table block, to control the data types of the table and breakpoint data, use one of these techniques:

  • Set the Value property of the embedded Simulink.lookuptable.Table and Simulink.lookuptable.Breakpoint objects by using untyped expressions such as [1 2 3], which returns a double vector. To control the data type, set the DataType property to a value other than auto.

    Use this technique to separate the value of the table or breakpoint data from the data type, which can improve readability and understanding of your design. You can then use a Simulink.NumericType or Simulink.AliasType object to:

    • Customize the name of the data type in the generated code.

    • Match the data type of the table or breakpoint data with the data type of a signal in the model.

  • Set the Value property of the embedded objects by using typed expressions such as single([1 2 3]). To use a fixed-point data type, set the Value property with an fi object.

    Set the DataType property of the embedded objects to the default value, auto. The table and breakpoint data then acquire the data type that you use to set the Value property.

    Use this technique to store the data type information in the Value property, which can simplify the way you interact with the Simulink.LookupTable and Simulink.Breakpoint objects. You can leave the DataType property at the default value.

    When you later change the breakpoint or table data in the Value property, preserve the data type information by using a typed expression. Alternatively, if you use a command at the command prompt or a script to change the data, to avoid using a typed expression, use subscripted assignment, (:).

    myLUTObject.Table.Value(:) = [4 5 6];

    When you change the data stored in the Value property, if you do not use a typed expression or subscripted assignment, you lose the data type information.

When blocks in a subsystem use Simulink.LookupTable or Simulink.Breakpoint objects, you cannot set data type override (see Control Fixed-Point Instrumentation and Data Type Override (Simulink)) only on the subsystem. Instead, set data type override on the entire model.

Data Typing for Expressions in Parameter Objects

You can use an expression to set the value of a parameter object (such as Simulink.Parameter). The expression encodes mathematical relationships between different objects. When you use this technique, different data typing rules apply. See Set Variable Value by Using a Mathematical Expression (Simulink).

Related Topics