This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Data Type Conversion

Convert input signal to specified data type

  • Library:
  • Simulink / Commonly Used Blocks

    Simulink / Signal Attributes

Description

The Data Type Conversion block converts an input signal of any Simulink® data type to the data type that you specify.

Note

To control the output data type by specifying block parameters, or to inherit a data type from a downstream block, use the Data Type Conversion block. To inherit a data type from a different signal in the model, use the Data Type Conversion Inherited block.

Convert Fixed-Point Signals

When you convert between fixed-point data types, the Input and output to have equal parameter controls block behavior. This parameter does not change the behavior of the block when:

  • The input and output do not have a fixed-point data type.

  • The input or output has a fixed-point data type with trivial scaling.

For more information about fixed-point numbers, see Fixed-Point Numbers in Simulink (Fixed-Point Designer).

To convert a signal from one data type to another by attempting to preserve the real-world value of the input signal, select Real World Value (RWV), the default setting. The block accounts for the limits imposed by the scaling of the input and output and attempts to generate an output of equal real-world value.

To change the real-world value of the input signal by performing a scaling reinterpretation of the stored integer value, select Stored Integer (SI). Within the limits of the specified data types, the block attempts to preserve the stored integer value of the signal during conversion. A best practice is to specify input and output data types using the same word length and signedness. Doing so ensures that the block changes only the scaling of the signal. Specifying a different signedness or word length for the input and output could produce unexpected results such as range loss or unexpected sign extensions. For an example, see Convert Data Types in Simulink Models.

If you select Stored Integer (SI), the block does not perform a lower-level bit reinterpretation of a floating-point input signal. For example, if the input is single and has value 5, the bits that store the input in memory are given in hexadecimal by the following command.

num2hex(single(5))
40a00000

However, the Data Type Conversion block does not treat the stored integer value as 40a00000, but instead as the real-world value, 5. After conversion, the stored integer value of the output is 5.

Cast Enumerated Signals

Use a Data Type Conversion block to cast enumerated signals as follows:

  1. To cast a signal of enumerated type to a signal of any numeric type.

    The underlying integers of all enumerated values input to the Data Type Conversion block must be within the range of the numeric type. Otherwise, an error occurs during simulation.

  2. To cast a signal of any integer type to a signal of enumerated type.

    The value input to the Data Type Conversion block must match the underlying value of an enumerated value. Otherwise, an error occurs during simulation.

    You can enable the Saturate on integer overflow parameter so that Simulink uses the default value of the enumerated type when the value input to the block does not match the underlying value of an enumerated value. See Type Casting for Enumerations (Simulink Coder).

You cannot use a Data Type Conversion block in these cases:

  • To cast a noninteger numeric signal to an enumerated signal.

  • To cast a complex signal to an enumerated signal, regardless of the data types of the real and imaginary parts of the complex signal.

See Simulink Enumerations for information on working with enumerated types.

Ports

Input

expand all

Input signal, specified as a scalar, vector, matrix, or N-D array. The input can be any real- or complex-valued signal. If the input is real, the output is real. If the input is complex, the output is complex. The block converts the input signal to the Output data type you specify.

When you are converting fixed-point data types, use the Input and output to have equal parameter to determine whether the conversion happens based on the Real World Value (RWV) or Stored Integer (SI) value of the signal. For more information, see Convert Fixed-Point Signals.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point | enumerated

Output

expand all

Output signal, converted to the data type you specify, with the same dimensions as the input signal.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point | enumerated

Parameters

expand all

Lower value of the output range that Simulink checks.

Simulink uses the minimum to perform:

Note

Output minimum does not saturate or clip the actual output signal. Use the Saturation block instead.

Programmatic Use

Block Parameter: OutMin
Type: character vector
Values: '[ ]'| scalar
Default: '[ ]'

Upper value of the output range that Simulink checks.

Simulink uses the maximum value to perform:

Note

Output maximum does not saturate or clip the actual output signal. Use the Saturation block instead.

Programmatic Use

Block Parameter: OutMax
Type: character vector
Values: '[ ]'| scalar
Default: '[ ]'

Choose the data type for the output. The type can be inherited, specified directly, or expressed as a data type object such as Simulink.NumericType.

Programmatic Use

Block Parameter: OutDataTypeStr
Type: character vector
Values: 'Inherit: Inherit via back propagation' | 'double' | 'single' | 'int8' | 'uint8' | 'int16' | 'uint16', 'int32' | 'uint32' | 'fixdt(1,16)' | 'fixdt(1,16,0)' | 'fixdt(1,16,2^0,0)' | 'Enum: <class name>''<data type expression>'
Default: 'Inherit: Inherit via back propagation'

Select this parameter to prevent the fixed-point tools from overriding the Output data type you specify on the block. For more information, see Use Lock Output Data Type Setting (Fixed-Point Designer).

Programmatic Use

Block Parameter: LockScale
Type: character vector
Values: 'off' | 'on'
Default: 'off'

Specify which type of input and output must be equal, in the context of fixed-point data representation.

  • Real World Value (RWV) — Specifies the goal of making the Real World Value (RWV) of the input equal to the Real World Value (RWV) of the output.

  • Stored Integer (SI) — Specifies the goal of making the Stored Integer (SI) value of the input equal to the Stored Integer (SI) value of the output.

Programmatic Use

Block Parameter: ConvertRealWorld
Type: character vector
Values: 'Real World Value (RWV)' | 'Stored Integer (SI)'
Default: 'Real World Value (RWV)'

Choose one of these rounding modes.

Ceiling

Rounds both positive and negative numbers toward positive infinity. Equivalent to the MATLAB® ceil function.

Convergent

Rounds number to the nearest representable value. If a tie occurs, rounds to the nearest even integer. Equivalent to the Fixed-Point Designer™ convergent function.

Floor

Rounds both positive and negative numbers toward negative infinity. Equivalent to the MATLAB floor function.

Nearest

Rounds number to the nearest representable value. If a tie occurs, rounds toward positive infinity. Equivalent to the Fixed-Point Designer nearest function.

Round

Rounds number to the nearest representable value. If a tie occurs, rounds positive numbers toward positive infinity and rounds negative numbers toward negative infinity. Equivalent to the Fixed-Point Designer round function.

Simplest

Automatically chooses between round toward floor and round toward zero to generate rounding code that is as efficient as possible.

Zero

Rounds number toward zero. Equivalent to the MATLAB fix function.

Programmatic Use

Block Parameter: RndMeth
Type: character vector
Values: 'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' | 'Round' | 'Simplest' | 'Zero'
Default: 'Floor'

See Also

For more information, see Rounding (Fixed-Point Designer).

Specify whether overflows saturate or wrap.

  • off — Overflows wrap to the appropriate value that the data type can represent.

    For example, the number 130 does not fit in a signed 8-bit integer and wraps to -126.

  • on — Overflows saturate to either the minimum or maximum value that the data type can represent.

    For example, an overflow associated with a signed 8-bit integer can saturate to -128 or 127.

Tip

  • Consider selecting this check box when your model has a possible overflow and you want explicit saturation protection in the generated code.

  • Consider clearing this check box when you want to optimize efficiency of your generated code.

    Clearing this check box also helps you to avoid overspecifying how a block handles out-of-range signals. For more information, see Check for Signal Range Errors.

  • When you select this check box, saturation applies to every internal operation on the block, not just the output or result.

  • In general, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.

Programmatic Use

Block Parameter: SaturateOnIntegerOverflow
Type: character vector
Values: 'off' | 'on'
Default: 'off'

Specify the sample time as a value other than -1. For more information, see Specify Sample Time.

Dependencies

This parameter is not visible unless it is explicitly set to a value other than -1. To learn more, see Blocks for Which Sample Time Is Not Recommended.

Programmatic Use

Block Parameter: SampleTime
Type: character vector
Values: scalar
Default: '-1'

Block Characteristics

Data Types

double | single | Boolean | base integer | fixed point | enumerated

Direct Feedthrough

No

Multidimensional Signals

Yes

Variable-Size Signals

Yes

Zero-Crossing Detection

No

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.

Fixed-Point Conversion
Convert floating-point algorithms to fixed point using Fixed-Point Designer™.

Introduced before R2006a