Products & Services Industries Academia Support User Community Company

Learn more about Simulink   

Working with Data Types

About Data Types

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 Real-Time Workshop 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.

Data Types Supported by Simulink

Simulink supports all built-in 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.

NameDescription

double

Double-precision floating point

single

Single-precision floating point

int8

Signed 8-bit integer

uint8

Unsigned 8-bit integer

int16

Signed 16-bit integer

uint16

Unsigned 16-bit integer

int32

Signed 32-bit integer

uint32

Unsigned 32-bit integer

Besides the built-in types, Simulink defines a boolean (1 or 0) type, instances of which are represented internally by uint8 values.

Many Simulink blocks also support fixed-point data types. See Blocks — Alphabetical List in the online documentation for information on the data types supported by specific blocks for parameter and input and output values. 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:

showblockdatatypetable

Fixed-Point Data

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 Simulink® Fixed Point™ product installed on your system. Specifically, you must have the product to:

If the Simulink Fixed Point 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 Simulink Fixed Point 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.

Overriding Fixed-Point Specifications

Most of the functionality in the Fixed-Point Tool is for use with the Simulink Fixed Point product. However, even if you do not have the Simulink Fixed Point product, you can use the tool's data type override mode to simulate a model that specifies fixed-point data types.

Data type override mode allows you to share fixed-point models with people in your company who do not have the Simulink Fixed Point product. In data type override mode, the fixed-point values are replaced with floating-point values when executing the model. Such replacement does not affect Fixed-Point Toolbox™ fi objects used as fixed-point parameters in your model. However, you can prevent the checkout of a Fixed-Point Toolbox license by setting the fipref DataTypeOverride property to TrueDoubles (see the Fixed-Point Toolbox documentation).

To simulate a model without using Simulink Fixed Point:

  1. From the Simulink Tools menu, select Fixed-Point > Fixed-Point Tool.

    The Fixed-Point Tool appears.

  2. Set the Fixed-point instrumentation mode parameter to Force off.

  3. Set the Data type override parameter to either True doubles or True singles.

Enumerated Data

An enumerated data type is a user-defined type whose values are strings that belong to a predefined set of strings. When the data type of an entity is an enumerated type, the value of the entity must be one of the strings that comprise the type. For information about enumerated data types, see Using Enumerated Data. Do not confuse enumerated data types with enumerated property types (see Enumerated Property Types).

Block Support for Data and Numeric Signal Types

All Simulink blocks accept signals of type double by default. Some blocks prefer boolean input and others support multiple data types on their inputs. See Simulink Blocks in Simulink Reference for information on the data types supported by specific blocks for parameter and input and output values. If the documentation for a block does not specify a data type, the block inputs or outputs only data of type double.

Creating Signals of a Specific Data Type

You can introduce a signal of a specific data type into a model in any of the following ways:

Specifying Block Output Data Types

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

Entering Valid Data Type Values

An assistant that helps you specify valid data type values

Using the Data Type Assistant

Specifying valid data type values for multiple blocks simultaneously

Using the Model Explorer for Batch Editing

Entering Valid Data Type Values

In general, you can specify the output data type as any of the following:

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 Reference.

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.

Inheritance RuleDescription
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 the accuracy and precision of the block output signal.

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:

Using the Model Explorer for Batch Editing

Using the Model Explorer (see The Model Explorer), you can assign the same output data type to multiple blocks simultaneously. For example, the sldemo_f14 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:

  1. Use the Model Explorer search bar (see Search Bar) to identify all blocks in the sldemo_f14 model of type Gain.

    The Model Explorer Contents pane lists all Gain blocks in the model.

  2. 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.

  3. 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.

  4. 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.

Using the Data Type Assistant

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:

Use the Data Type Assistant to specify a data type as follows:

  1. In the Mode field, select the category of data type that you want to specify. In general, the options include the following:

    ModeDescription

    Inherit

    Inheritance rules for data types

    Built in

    Built-in data types

    Fixed point

    Fixed-point data types

    Expression

    Expressions that evaluate to data types

    Bus Object

    Simulink.Bus object name, valid only for the Embedded MATLAB™ Function block and the Stateflow Chart block

    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.

  2. 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.

  3. 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. See Specifying Fixed-Point Data Types with the Data Type Assistant in the Simulink Fixed Point User's Guide for details about specifying fixed-point data types.

Specifying a Fixed-Point Data Type

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 Concepts in the Simulink Fixed Point User's Guide. 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:

Scaling ModeDescription
Binary point

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.

For more information about fixed-point scaling, see Scaling in the Simulink Fixed Point User's Guide.

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 in the Simulink Fixed Point User's Guide.

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. See Scaling in the Simulink Fixed Point User's Guide for instructions on autoscaling fixed-point data.

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 Checking Signal Ranges and Checking 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. See Fixed-Point Concepts in the Simulink Fixed Point User's Guide for information about these three quantities.

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.

Displaying Port Data Types

To display the data types of ports in your model, select Format > Port/Signal Displays > 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.

Data Type Propagation

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.

Data Typing Rules

Observing the following rules can help you to create models that are typesafe and, therefore, execute without error:

Typecasting 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.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS