|On this page…|
The term data type refers to the way in which a computer represents numbers in memory. A data type determines the amount of storage allocated to a number, the method used to encode the number's value as a pattern of binary digits, and the operations available for manipulating the type. Most computers provide a choice of data types for representing numbers, each with specific advantages in the areas of precision, dynamic range, performance, and memory usage. To optimize performance, you can specify the data types of variables used in the MATLAB® technical computing environment. The Simulink® software builds on this capability by allowing you to specify the data types of Simulink signals and block parameters.
The ability to specify the data types of a model's signals and block parameters is particularly useful in real-time control applications. For example, it allows a Simulink model to specify the optimal data types to use to represent signals and block parameters in code generated from a model by automatic code-generation tools, such as the Simulink Coder™ product. By choosing the most appropriate data types for your model's signals and parameters, you can dramatically increase performance and decrease the size of the code generated from the model.
Simulink performs extensive checking before and during a simulation to ensure that your model is typesafe, that is, that code generated from the model will not overflow or underflow and thus produce incorrect results. Simulink models that use the default data type (double) are inherently typesafe. Thus, if you never plan to generate code from your model or use a nondefault data type in your models, you can skip the remainder of this section.
On the other hand, if you plan to generate code from your models and use nondefault data types, read the remainder of this section carefully, especially the section on data type rules (see Data Typing Rules). In that way, you can avoid introducing data type errors that prevent your model from running to completion or simulating at all.
Simulink supports all built-in numeric MATLAB data types except int64 and uint64. The term built-in data type refers to data types defined by MATLAB itself as opposed to data types defined by MATLAB users. Unless otherwise specified, the term data type in the Simulink documentation refers to built-in data types. The following table lists the built-in MATLAB data types supported by Simulink.
Double-precision floating point
Single-precision floating point
Signed 8-bit integer
Unsigned 8-bit integer
Signed 16-bit integer
Unsigned 16-bit integer
Signed 32-bit integer
Unsigned 32-bit integer
Besides these built-in types, Simulink defines a boolean (true or false) type. The values 1 and 0 represent true and false respectively. For this data type, Simulink represents real, nonzero numeric values (including Inf) as true (1).
Several blocks support bus objects (Simulink.Bus) as data types. See Bus Objects.
Many Simulink blocks also support fixed-point data types. For more information on the data types supported by a specific block for parameter and input and output values, in the Simulink documentation see the Data Type Support section of the reference page for that block. If the documentation for a block does not specify a data type, the block inputs or outputs only data of type double.
To view a table that summarizes the data types supported by the blocks in the Simulink block libraries, execute the following command at the MATLAB command line:
The Simulink software allows you to create models that use fixed-point numbers to represent signals and parameter values. Use of fixed-point data can reduce the memory requirements and increase the speed of code generated from a model.
To execute a model that uses fixed-point numbers, you must have the Fixed-Point Designer™ product installed on your system. Specifically, you must have the product to:
Update a Simulink diagram (Ctrl+D) containing fixed-point data types
Run a model containing fixed-point data types
Generate code from a model containing fixed-point data types
Log the minimum and maximum values produced by a simulation
Automatically scale the output of a model using the autoscaling tool
If the Fixed-Point Designer product is not installed on your system, you can execute a fixed-point model as a floating-point model by enabling automatic conversion of fixed-point data to floating-point data during simulation. See Overriding Fixed-Point Specifications for details.
If you do not have the Fixed-Point Designer product installed and do not enable automatic conversion of fixed-point to floating-point data, an error occurs if you try to execute a fixed-point model.
Note: You do not need the Fixed-Point Designer product to edit a model containing fixed-point blocks, or to use the Data Type Assistant to specify fixed-point data types, as described in Specifying a Fixed-Point Data Type.
Most of the functionality in the Fixed-Point Tool is for use with the Fixed-Point Designer software. However, even if you do not have Fixed-Point Designer software, you can configure data type override settings to simulate a model that specifies fixed-point data types. In this mode, the Simulink software temporarily overrides fixed-point data types with floating-point data types when simulating the model.
Note: If you use fi objects or embedded numeric data types in your model or workspace, you might introduce fixed-point data types into your model. You can set fipref to prevent the checkout of a Fixed-Point Designer license.
To simulate a model without using Fixed-Point Designer:
In the Model Hierarchy pane, select the root model.
The Fixed-Point Tool opens.
In the section Settings for selected system:
If you use fi objects or embedded numeric data types in your model, set the fipref DataTypeOverride property to TrueDoubles or TrueSingles (to be consistent with the model-wide data type override setting) and the DataTypeOverrideAppliesTo property to All numeric types.
For example, at the MATLAB command line, enter:
p = fipref('DataTypeOverride', 'TrueDoubles', ... 'DataTypeOverrideAppliesTo', 'AllNumericTypes');
An enumeration is a user-defined data type with a fixed set of names that represent a single type of value. When the data type of an entity is an enumeration, the value of the entity must be one of the values defined that enumeration. For information about enumerations, see Data Types.
A bus object (Simulink.Bus) specifies the architectural properties of a bus, as distinct from the values of the signals it contains. For example, a bus object can specify the number of elements in a bus, the order of those elements, whether and how elements are nested, and the data types of constituent signals; but not the signal values.
You can specify a bus object as a data type for the following blocks:
You can specify a bus object as a data type for the following classes:
See Specify a Bus Object Data Type for information about how to specify a bus object as a data type for blocks and classes.
All Simulink blocks accept signals of type double by default. Some blocks prefer boolean input and others support multiple data types on their inputs. For more information on the data types supported by a specific block for parameter and input and output values, in the Simulink documentation see the Data Type Support section of the reference page for that bock. If the documentation for a block does not specify a data type, the block inputs or outputs only data of type double.
You can introduce a signal of a specific data type into a model in any of the following ways:
Simulink blocks determine the data type of their outputs by default. Many blocks allow you to override the default type and explicitly specify an output data type, using a block parameter that is typically named Output data type. For example, the Output data type parameter appears on the Signal Attributes pane of the Constant block dialog box.
See the following topics for more information:
|For Information About...||See...|
Valid data type values that you can specify
An assistant that helps you specify valid data type values
Specifying valid data type values for multiple blocks simultaneously
In general, you can specify the output data type as any of the following:
A rule that inherits a data type (see Data Type Inheritance Rules)
The name of a built-in data type (see Built-In Data Types)
An expression that evaluates to a data type (see Data Type Expressions)
Valid data type values vary among blocks. You can use the pull-down menu associated with a block data type parameter to view the data types that a particular block supports. For example, the Data type pull-down menu on the Data Store Memory block dialog box lists the data types that it supports, as shown here.
For more information about the data types that a specific block supports, see the documentation for the block in the Simulink documentation.
Data Type Inheritance Rules. Blocks can inherit data types from a variety of sources, including signals to which they are connected and particular block parameters. You can specify the value of a data type parameter as a rule that determines how the output signal inherits its data type. To view the inheritance rules that a block supports, use the data type pull-down menu on the block dialog box. The following table lists typical rules that you can select.
|Inherit: Inherit via back propagation|
Simulink automatically determines the output data type of the block during data type propagation (see Data Type Propagation). In this case, the block uses the data type of a downstream block or signal object.
|Inherit: Same as input|
The block uses the data type of its sole input signal for its output signal.
|Inherit: Same as first input|
The block uses the data type of its first input signal for its output signal.
|Inherit: Same as second input|
The block uses the data type of its second input signal for its output signal.
|Inherit: Inherit via internal rule|
The block uses an internal rule to determine its output data type. The internal rule chooses a data type that optimizes numerical accuracy, performance, and generated code size, while taking into account the properties of the embedded target hardware. It is not always possible for the software to optimize efficiency and numerical accuracy at the same time.
Built-In Data Types. You can specify the value of a data type parameter as the name of a built-in data type, for example, single or boolean. To view the built-in data types that a block supports, use the data type pull-down menu on the block dialog box. See Data Types Supported by Simulink for a list of all built-in data types that are supported.
Data Type Expressions. You can specify the value of a data type parameter as an expression that evaluates to a numeric data type object. Simply enter the expression in the data type field on the block dialog box. In general, enter one of the following expressions:
Specify the value of a data type parameter as a command that invokes the fixdt function. This function allows you to create a Simulink.NumericType object that describes a fixed-point or floating-point data type.
Data Type Object Name
Specify the value of a data type parameter as the name of a data object that represents a data type. Simulink data objects that you instantiate from classes, such as Simulink.NumericType and Simulink.AliasType, simplify the task of making model-wide changes to output data types and allow you to use custom aliases for data types. See Data Objects for more information about Simulink data objects.
Using the Model Explorer (see Model Explorer Overview), you can assign the same output data type to multiple blocks simultaneously. For example, the slexAircraftExample model that comes with the Simulink product contains numerous Gain blocks. Suppose you want to specify the Output data type parameter of all the Gain blocks in the model as single. You can achieve this task as follows:
Use the Model Explorer search bar (see Search Using Model Explorer) to identify all blocks in the slexAircraftExample model of type Gain.
The Model Explorer Contents pane lists all Gain blocks in the model.
In the Model Explorer Contents pane, select all the Gain blocks whose Output data type parameter you want to specify.
Model Explorer highlights the rows corresponding to your selections.
In the Model Explorer Contents pane, click the data type associated with one of the selected Gain blocks.
Model Explorer displays a pull-down menu with valid data type options.
In the pull-down menu, enter or select the desired data type, for example, single.
Model Explorer specifies the data type of all selected items as single.
The Data Type Assistant is an interactive graphical tool that simplifies the task of specifying data types for blocks and data objects. The assistant appears on block and object dialog boxes, adjacent to parameters that provide data type control, such as the Output data type parameter. For example, it appears on the Signal Attributes pane of the Constant block dialog box shown here.
You can selectively show or hide the Data Type Assistant by clicking the applicable button:
Click the Show data type assistant button to display the assistant.
Click the Hide data type assistant button to hide a visible assistant.
Use the Data Type Assistant to specify a data type as follows:
In the Mode field, select the category of data type that you want to specify. In general, the options include the following:
Inheritance rules for data types
Built-in data types
Fixed-point data types
Enumerated data types
Bus object data types
Expressions that evaluate to data types
The assistant changes dynamically to display different options that correspond to the selected mode. For example, setting Mode to Expression causes the Constant block dialog box to appear as follows.
In the field that is to the right of the Mode field, select or enter a data type.
For example, suppose that you designate the variable myDataType as an alias for a single data type. You create an instance of the Simulink.AliasType class and set its BaseType property by entering the following commands:
myDataType = Simulink.AliasType myDataType.BaseType = 'single'
You can use this data type object to specify the output data type of a Constant block. Enter the data type alias name, myDataType, as the value of the expression in the assistant.
Click the OK or Apply button to apply your changes.
The assistant uses the data type that you specified to populate the associated data type parameter in the block or object dialog box. In the following example, the Output data type parameter of the Constant block specifies the same expression that you entered using the assistant.
For more information about the data types that you can specify using the Data Type Assistant, see Entering Valid Data Type Values. For details about specifying fixed-point data types, see Specify Fixed-Point Data Types with the Data Type Assistant.
When the Data Type Assistant Mode is Fixed point, the Data Type Assistant displays fields for specifying information about your fixed-point data type. For a detailed discussion about fixed-point data, see Fixed-Point Basics. For example, the next figure shows the Block Parameters dialog box for a Gain block, with the Signal Attributes tab selected and a fixed-point data type specified.
If the Scaling is Slope and bias rather than Binary point, the Data Type Assistant displays a Slope field and a Bias field rather than a Fraction length field:
You can use the Data Type Assistant to set these fixed-point properties:
Signedness. Specify whether you want the fixed-point data to be Signed or Unsigned. Signed data can represent positive and negative values, but unsigned data represents positive values only. The default setting is Signed.
Word length. Specify the bit size of the word that will hold the quantized integer. Large word sizes represent large values with greater precision than small word sizes. Word length can be any integer between 0 and 32. The default bit size is 16.
Scaling. Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors. The default method is Binary point scaling. You can select one of two scaling modes:
If you select this mode, the Data Type Assistant displays the Fraction Length field, which specifies the binary point location.
Binary points can be positive or negative integers. A positive integer moves the binary point left of the rightmost bit by that amount. For example, an entry of 2 sets the binary point in front of the second bit from the right. A negative integer moves the binary point further right of the rightmost bit by that amount, as in this example:
The default binary point is 0.
|Slope and bias|
If you select this mode, the Data Type Assistant displays fields for entering the Slope and Bias.
Slope can be any positive real number, and the default slope is 1.0. Bias can be any real number, and the default bias is 0.0. You can enter slope and bias as expressions that contain parameters you define in the MATLAB workspace.
Note Use binary-point scaling whenever possible to simplify the implementation of fixed-point data in generated code. Operations with fixed-point data using binary-point scaling are performed with simple bit shifts and eliminate expensive code implementations, which are required for separate slope and bias values.
For more information about fixed-point scaling, see Scaling.
Data type override. When the Mode is Built in or Fixed point, you can use the Data type override option to specify whether you want this data type to inherit or ignore the data type override setting specified for its context, that is, for the block, Simulink.Signal object or Stateflow® chart in Simulink that is using the signal. The default behavior is Inherit.
|Data Type Override Mode||Description|
Inherits the data type override setting from its context, that is, from the block, Simulink.Signal object or Stateflow chart in Simulink that is using the signal.
Ignores the data type override setting of its context and uses the fixed-point data type specified for the signal.
The ability to turn off data type override for an individual data type provides greater control over the data types in your model when you apply data type override. For example, you can use this option to ensure that data types meet the requirements of downstream blocks regardless of the data type override setting.
Calculate Best-Precision Scaling. Click this button to calculate best-precision values for both Binary point and Slope and bias scaling, based on the specified minimum and maximum values. The Simulink software displays the scaling values in the Fraction Length field or the Slope and Bias fields. For more information, see Constant Scaling for Best Precision.
Showing Fixed-Point Details. When you specify a fixed-point data type, you can use the Fixed-point details subpane to see information about the fixed-point data type that is currently displayed in the Data Type Assistant. To see the subpane, click the expander next to Fixed-point details in the Data Type Assistant. The Fixed-point details subpane appears at the bottom of the Data Type Assistant:
The rows labeled Output minimum and Output maximum show the same values that appear in the corresponding Output minimum and Output maximum fields above the Data Type Assistant. The names of these fields may differ from those shown. For example, a fixed-point block parameter would show Parameter minimum and Parameter maximum, and the corresponding Fixed-point details rows would be labeled accordingly. See Signal Ranges and Check Parameter Values for more information.
The rows labeled Representable minimum, Representable maximum, and Precision always appear. These rows show the minimum value, maximum value, and precision that can be represented by the fixed-point data type currently displayed in the Data Type Assistant. For information about these three quantities, see Fixed-Point Basics.
The values displayed by the Fixed-point details subpane do not automatically update if you click Calculate Best-Precision Scaling, or change the range limits, the values that define the fixed-point data type, or anything elsewhere in the model. To update the values shown in the Fixed-point details subpane, click Refresh Details. The Data Type Assistant then updates or recalculates all values and displays the results.
Clicking Refresh Details does not change anything in the model, it only changes the display. Click OK or Apply to put the displayed values into effect. If the value of a field cannot be known without first compiling the model, the Fixed-point details subpane shows the value as Unknown.
If any errors occur when you click Refresh Details, the Fixed-point details subpane shows an error flag on the left of the applicable row, and a description of the error on the right. For example, the next figure shows two errors:
The row labeled Output minimum shows the error Cannot evaluate because evaluating the expression MySymbol, specified in the Output minimum field, did not return an appropriate numeric value. When an expression does not evaluate successfully, the Fixed-point details subpane displays the unevaluated expression (truncating to 10 characters if necessary to save space) in place of the unavailable value.
To correct the error in this case, you would need to define MySymbol in an accessible workspace to provide an appropriate numeric value. After you clicked Refresh Details, the value of MySymbol would appear in place of its unevaluated text, and the error indicator and error description would disappear.
To correct the error shown for Output maximum, you would need to decrease Output maximum, increase Word length, or decrease Fraction length (or some combination of these changes) sufficiently to allow the fixed-point data type to represent the maximum value that it could have.
Other values relevant to a particular block can also appear in the Fixed-point details subpane. For example, on a Discrete-Time Integrator block's Signal Attributes tab, the subpane could look like this:
Note that the values displayed for Upper saturation limit and Lower saturation limit are greyed out. This appearance indicates that the corresponding parameters are not currently used by the block. The greyed-out values can be ignored.
Note also that Initial condition displays the value 1..4. The actual value is a vector or matrix whose smallest element is 1 and largest element is 4. To conserve space, the Fixed-point details subpane shows only the smallest and largest element of a vector or matrix. An ellipsis (..) replaces the omitted values. The underlying definition of the vector or matrix is unaffected.
Lock output data type setting against changes by the fixed-point tools. Select this check box to prevent replacement of the current data type with a type that the Fixed-Point Tool or Fixed-Point Advisor chooses. For instructions on autoscaling fixed-point data, see Scaling.
You can specify an enumerated data type by selecting the Enum: <class name> option and specify an enumerated object.
In the Data Type Assistant, you can use the Mode parameter to specify a bus as a data object for a block. Select the Enumerated option and specify an enumerated object.
For details about enumerated data types, see Data Types.
The blocks listed in the section called Bus Objects support your specifying a bus object as a data type. For those blocks, in the Data type parameter, select the Bus: <object name> option and specify a bus object. You cannot use the Expression option to specify a bus object as a data type for a block.
In the Data Type Assistant, you can use the Mode parameter to specify a bus as a data object for a block. Select the Bus option and specify a bus object.
You can specify a bus object as the data type for data objects such as Simulink.Signal, Simulink.Parameter, and Simulink.BusElement. In the Model Explorer, in Properties dialog box for a data object, in the Data type parameter, select the Bus: <object name> option and specify a bus object. You can also use the Expression option to specify a bus object.
For more information on specifying a bus object data type, see Associating Bus Objects with Simulink Blocks
In the Simulink Editor, select Display > Signals & Ports > Port Data Types. The port data type display is not automatically updated when you change the data type of a diagram element. To refresh the display, press Ctrl+D.
Whenever you start a simulation, enable display of port data types, or refresh the port data type display, the Simulink software performs a processing step called data type propagation. This step involves determining the types of signals whose type is not otherwise specified and checking the types of signals and input ports to ensure that they do not conflict. If type conflicts arise, an error dialog is displayed that specifies the signal and port whose data types conflict. The signal path that creates the type conflict is also highlighted.
Note You can insert typecasting (data type conversion) blocks in your model to resolve type conflicts. See Typecast Signals for more information.
Observing the following rules can help you to create models that are typesafe and, therefore, execute without error:
Signal data types generally do not affect parameter data types, and vice versa.
A significant exception to this rule is the Constant block, whose output data type is determined by the data type of its parameter.
If the output of a block is a function of an input and a parameter, and the input and parameter differ in type, the Simulink software converts the parameter to the input type before computing the output.
In general, a block outputs the data type that appears at its inputs.
Significant exceptions include Constant blocks and Data Type Conversion blocks, whose output data types are determined by block parameters.
Virtual blocks accept signals of any type on their inputs.
Examples of virtual blocks include Mux and Demux blocks and unconditionally executed subsystems.
The elements of a signal array connected to a port of a nonvirtual block must be of the same data type.
The signals connected to the input data ports of a nonvirtual block cannot differ in type.
Control ports (for example, Enable and Trigger ports) accept any data type.
Solver blocks accept only double signals.
An error is displayed whenever it detects that a signal is connected to a block that does not accept the signal's data type. If you want to create such a connection, you must explicitly typecast (convert) the signal to a type that the block does accept. You can use the Data Type Conversion block to perform such conversions.
You can use data type override mode to switch the data types in your model. This capability allows you to maintain one model but simulate and generate code for multiple data types, and also validate the numerical behavior for each type. For example, if you implement an algorithm using double-precision data types and want to check whether the algorithm is also suitable for single-precision use, you can apply a data type override to floating-point data types to replace all doubles with singles without affecting any other data types in your model.
To apply a data type override, you must specify the data type that you want to apply and the data type that you want to replace.
You can set a data type override using one of the following methods. In these examples, both methods change all floating-point data types to single.
set_param(gcs, 'DataTypeOverride', 'Single'); set_param(gcs, 'DataTypeOverrideAppliesTo','Floating-point');
In the Simulink Editor, select Analysis > Fixed-Point Tool.
The Fixed-Point Tool opens.
Select View > Show Settings for Selected System.
In the Fixed-Point Tool right pane, under Settings for selected system:
Set Data type override to Single.
Set Data type override applies to Floating-point.
For more information on data type override settings, see fxptdlg.
This example uses the ex_single_validation model to show how you can use data type overrides. It proves that an algorithm, which implements double-precision data types, is also suitable for single-precision embedded use.
The inputs In2 and In3 are double-precision inputs to the Sum and Product blocks.
The outputs of the Sum and Product blocks are data inputs to the Multiport Switch block.
The input In1 is the control input to the Multiport Switch block. The value of this control input determines which of its other inputs, the sum of In2 and In3 or the product of In2 and In3, passes to the output port. Because In1 is a control input, its data type is int8.
The Relational Operator block compares the output of the Multiport Switch block to In4, and outputs a Boolean signal.
Open the ex_single_validation model. At the MATLAB command line, enter:
From the model menu, select Analysis > Model Advisor > Model Advisor.
In the System Selector dialog box, click OK.
The Model Advisor opens.
In the Model Advisor, expand the By Task node and, under Modeling Single-Precision Systems, select the Identify questionable operations for strict single-precison design check.
In the right pane, click Run This Check.
The check generates a warning that multiple blocks use double-precision floating-point operations.
In the Simulink Editor, select Analysis > Fixed-Point Tool.
The Fixed-Point Tool opens.
In the Fixed-Point Tool right pane, under Settings for selected system:
Set Data type override to Single.
Set Data type override applies to Floating-point.
In the model menu, select Simulation > Update Diagram.
The data type override replaces all the floating-point (double) data types in the model with single data types, but does not affect the integer or Boolean data types.
The check passes indicating that this algorithm is suitable for single-precision embedded use.