Control Block Parameter Data Types - MATLAB & Simulink (original) (raw)

A block parameter, such as the Gain parameter of aGain 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:

Reduce Maintenance Effort with Data Type Inheritance

By default, block parameters, numeric MATLAB variables that use the data type double, andSimulink.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 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 aSimulink.Parameter object, set theValue property by using a double value and set the DataType property toauto (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.

  1. Use a Simulink.Bus object as the data type of the structure.
  2. 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 aGain 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 theDataType property of the object to specify a data type.

If you use model arguments, you can specify a data type:

The default settings for these data types typically use inheritance and context-sensitive data typing. For example, the default value of theDataType 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, clickModel 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 toDesign and specify data types by using theData 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:

For basic information about fixed-point data types, block parameters, and other tools and concepts, use the information in the table.

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:

For more information about using the Fixed-Point Tool to autoscaleSimulink.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:

  1. On the Data Types tab of an n-D Lookup Table block, under the Minimum andMaximum columns, specify a value range for the elements of the table data.
  2. Under the Data Type column, set the table data type to fixdt(1,16).
  3. 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 theDataType property of the object toauto (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 value15.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.

  1. At the command prompt, create a parameter object in the base workspace whose value is 15.25.
    myParam = Simulink.Parameter(15.25);
  2. In the MATLAB Workspace panel, double-click the objectmyParam. The property dialog box opens.
  3. Specify range information in the object. For example, setMinimum to 0.00 andMaximum to32.00.
  4. Set Data type tofixdt(0,16,0).
  5. 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 aGain 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 > > > . Set the values of these diagnostic configuration parameters towarning or error:

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:

Use these techniques to match your custom data types:

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 aMATLAB 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 anddoubleParam, and use the objects to specify theConstant 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 theConstant value parameter using these steps:

  1. 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)
  2. 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 expression int8(3) * double(9.36) isint8(28).

If you use an expression to set the value of a parameter object (such asSimulink.Parameter), parameter objects used in the expression follow different data typing rules. The auto setting of theDataType property has a slightly different meaning. SeeSet 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).

See Also

Topics