Constant

Generate constant value

Library

Sources

Description

The Constant block generates a real or complex constant value.

The block generates scalar, vector, or matrix output, depending on:

  • The dimensionality of the Constant value parameter

  • The setting of the Interpret vector parameters as 1-D parameter

Also, the block can generate either a sample-based or frame-based signal, depending on the setting of the Sampling mode.

The output of the block has the same dimensions and elements as the Constant value parameter. If you specify for this parameter a vector that you want the block to interpret as a vector, select the Interpret vector parameters as 1-D parameter. Otherwise, if you specify a vector for the Constant value parameter, the block treats that vector as a matrix.

Data Type Support

By default, the Constant block outputs a signal whose data type and complexity are the same as those of the Constant value parameter. However, you can specify the output to be any data type that Simulink® supports, including fixed-point and enumerated data types. The Enumerated Constant block can be more convenient than the Constant block for outputting a constant enumerated value. You can also use a bus object as the output data type, which can help to simplify a model (see Bus Support for details).

    Note:   If you specify a bus object as the data type for this block, do not set the minimum and maximum values for bus data on the block. Simulink ignores these settings. Instead, set the minimum and maximum values for bus elements of the bus object specified as the data type. The values should be finite real double scalar.

    For information on the Minimum and Maximum properties of a bus element, see Simulink.BusElement.

For more information about data type support, see Data Types Supported by Simulink in the Simulink documentation.

Parameters and Dialog Box

The Main pane of the Constant block dialog box appears as follows:

The Signal Attributes pane of the Constant block dialog appears as follows:

Constant value

Specify the constant value output of the block.

Settings

Default: 1

Minimum: value from the Output minimum parameter

Maximum: value from the Output maximum parameter

  • You can enter any expression that MATLAB® evaluates as a matrix, including the Boolean keywords true and false.

  • If you set the Output data type to be a bus object, you can specify either:

    • A full MATLAB structure corresponding to the bus object

    • 0 to indicate a structure corresponding to the ground value of the bus object

    For details, see Bus Support.

  • For non-bus data types, Simulink converts this parameter from its value data type to the specified output data type offline, using round toward nearest and saturation.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Interpret vector parameters as 1-D

Select this check box to output a vector of length N if the Constant value parameter evaluates to an N-element row or column vector.

Settings

Default: On

On

Outputs a vector of length N if the Constant value parameter evaluates to an N-element row or column vector. For example, the block outputs a matrix of dimension 1-by-N or N-by-1.

Off

Does not output a vector of length N if the Constant value parameter evaluates to an N-element row or column vector.

If you clear this check box, you can interact with the Sampling mode parameter.

Dependencies

This parameter enables Sampling mode.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Sampling mode

Specify whether the output signal is Sample based or Frame based.

Settings

Default: Sample based

Sample based

The output signal is sample-based.

Frame based

The output signal is frame-based.

Tip

If you specify the Data type to be a bus object, do not specify a frame-based sampling mode.

Dependencies

  • Frame-based processing requires a DSP System Toolbox™ license.

    For more information, see Sample- and Frame-Based Concepts in the DSP System Toolbox documentation.

  • Clearing Interpret vector parameters as 1-D enables this parameter.

  • Selecting Sample based enables the following parameter:

    • Sample time

  • Selecting Frame based enables the following parameter:

    • Frame period

Command-Line Information

See Block-Specific Parameters for the command-line information.

Sample time

Specify the interval between times that the Constant block output can change during simulation (for example, due to tuning the Constant value parameter).

Settings

Default: inf

This setting indicates that the block output can never change. This setting speeds simulation and generated code by avoiding the need to recompute the block output.

See Specify Sample Time in the online documentation for more information.

Dependency

Sampling mode enables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Output minimum

Lower value of the output range that Simulink checks.

Settings

Default: [] (unspecified)

Specify this number as a finite, real, double, scalar value.

    Note:   If you specify a bus object as the data type for this block, do not set the minimum value for bus data on the block. Simulink ignores this setting. Instead, set the minimum values for bus elements of the bus object specified as the data type. For information on the Minimum parameter for a bus element, see Simulink.BusElement.

Simulink uses the minimum to perform:

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

Command-Line Information

Parameter: OutMin
Type: string
Value: '[ ]'
Default: '[ ]'

Output maximum

Upper value of the output range that Simulink checks.

Settings

Default: [] (unspecified)

Specify this number as a finite, real, double, scalar value.

    Note:   If you specify a bus object as the data type for this block, do not set the maximum value for bus data on the block. Simulink ignores this setting. Instead, set the maximum values for bus elements of the bus object specified as the data type. For information on the Maximum parameter for a bus element, see Simulink.BusElement.

Simulink uses the maximum value to perform:

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

Command-Line Information

Parameter: OutMax
Type: string
Value: '[ ]'
Default: '[ ]'

Scaling

Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors.

Settings

Default: Best precision

Binary point

Specify binary point location.

Slope and bias

Enter slope and bias.

Best precision

Specify best-precision values.

Dependencies

Selecting Mode > Fixed point enables this parameter.

Selecting Binary point enables:

  • Fraction length

  • Calculate Best-Precision Scaling

Selecting Slope and bias enables:

  • Slope

  • Bias

  • Calculate Best-Precision Scaling

See Also

For more information, see Specifying a Fixed-Point Data Type.

Output data type

Specify the output data type.

Settings

Default: Inherit: Inherit from 'Constant value'

Inherit: Inherit from 'Constant value'

Use data type of Constant value.

Inherit: Inherit via back propagation

Use data type of the driving block.

double

Output data type is double.

single

Output data type is single.

int8

Output data type is int8.

uint8

Output data type is uint8.

int16

Output data type is int16.

uint16

Output data type is uint16.

int32

Output data type is int32.

uint32

Output data type is uint32.

boolean

Output data type is boolean.

fixdt(1,16)

Output data type is fixed point fixdt(1,16).

fixdt(1,16,0)

Output data type is fixed point fixdt(1,16,0).

fixdt(1,16,2^0,0))

Output data type is fixed point fixdt(1,16,2^0,0).

Enum: <class name>

Use an enumerated data type, for example, Enum: BasicColors.

Bus: <object name>

Data type is a bus object.

<data type expression>

Data type is data type object, for example Simulink.NumericType.

Do not specify a bus object as the expression.

Command-Line Information

See Block-Specific Parameters for the command-line information.

See Also

See Specify Block Output Data Types for more information.

Show data type assistant

Display the Data Type Assistant.

Settings

The Data Type Assistant helps you set the Output data type parameter.

For more information, see Specify Block Output Data Types.

Mode

Select the category of data to specify.

Settings

Default: Inherit

Inherit

Inheritance rules for data types. Selecting Inherit enables a second menu/text box to the right. Select one of the following choices:

  • Inherit from 'Constant value' (default)

  • Inherit via back propagation

Built in

Built-in data types. Selecting Built in enables a second menu/text box to the right. Select one of the following choices:

  • double (default)

  • single

  • int8

  • uint8

  • int16

  • uint16

  • int32

  • uint32

  • boolean

Fixed point

Fixed-point data types.

Enumerated

Enumerated data types. Selecting Enumerated enables a second menu/text box to the right, where you can enter the class name.

Bus

Bus object. Selecting Bus enables a Bus object parameter to the right, where you enter the name of a bus object that you want to use to define the structure of the bus. If you need to create or change a bus object, click Edit to the right of the Bus object field to open the Simulink Bus Editor. For details about the Bus Editor, see Manage Bus Objects with the Bus Editor.

Expression

Expressions that evaluate to data types. Selecting Expression enables a second menu/text box to the right, where you can enter the expression.

Do not specify a bus object as the expression.

Frame period

Specify the interval between frames that the Constant block output can change during simulation (for example, due to tuning the Constant value parameter).

Settings

Default: inf

Dependency

Sampling mode enables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Dependency

Clicking the Show data type assistant button enables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

See Also

See Specify Data Types Using Data Type Assistant.

Data type override

Specify data type override mode for this signal.

Settings

Default: Inherit

Inherit

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.

Off

Ignores the data type override setting of its context and uses the fixed-point data type specified for the signal.

Tip

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.

Dependency

This parameter appears only when the Mode is Built in or Fixed point.

Signedness

Specify whether you want the fixed-point data as signed or unsigned.

Settings

Default: Signed

Signed

Specify the fixed-point data as signed.

Unsigned

Specify the fixed-point data as unsigned.

Dependencies

Selecting Mode > Fixed point enables this parameter.

See Also

For more information, see Specifying a Fixed-Point Data Type.

Word length

Specify the bit size of the word that holds the quantized integer.

Settings

Default: 16

Minimum: 0

Maximum: 32

Dependencies

Selecting Mode > Fixed point enables this parameter.

See Also

For more information, see Specifying a Fixed-Point Data Type.

Fraction length

Specify fraction length for fixed-point data type.

Settings

Default: 0

Binary points can be positive or negative integers.

Dependencies

Selecting Scaling > Binary point enables this parameter.

See Also

For more information, see Specifying a Fixed-Point Data Type.

Slope

Specify slope for the fixed-point data type.

Settings

Default: 2^0

Specify any positive real number.

Dependencies

Selecting Scaling > Slope and bias enables this parameter.

See Also

For more information, see Specifying a Fixed-Point Data Type.

Bias

Specify bias for the fixed-point data type.

Settings

Default: 0

Specify any real number.

Dependencies

Selecting Scaling > Slope and bias enables this parameter.

See Also

For more information, see Specifying a Fixed-Point Data Type.

Lock output data type setting against changes by the fixed-point tools

Select to lock the output data type setting of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor.

Settings

Default: Off

On

Locks the output data type setting for this block.

Off

Allows the Fixed-Point Tool and the Fixed-Point Advisor to change the output data type setting for this block.

Command-Line Information

Parameter: LockScale
Type: string
Value: 'off' | 'on'
Default: 'off'

See Also

For more information, see Use Lock Output Data Type Setting.

Bus Support

Using Bus Objects as the Output Data Type

The Constant block supports nonvirtual buses as the output data type. If you use a bus object as the data type, set Constant value to 0 or a MATLAB structure that matches the bus object.

Using Structures for the Constant Value

The structure you specify must contain a value for every element of the bus represented by the bus object.

You can use the Simulink.Bus.createMATLABStruct to create a full structure that corresponds to a bus.

You can use Simulink.Bus.createObject to create a bus object from a MATLAB structure.

Sampling Mode

Do not specify a frame-based sampling mode if you specify the Data type to be a bus object.

Example of Using a Bus Object for a Constant Block

The following example illustrates how using a bus object as an output data type for a Constant block can help to simplify a model.

  1. Open the busic_examplebusic_example model and update it.

    This model uses six Constant blocks. For details about the model, see Examples of Partial Structures.

  2. Open the constantbus_exampleconstantbus_example model and update it. This model uses one Constant block that replaces the six Constant blocks in the busic_example model.

  3. Simulate the constantbus_example model. To verify that the output from the Constant block reflects the values from constant_value_struct, perform the next two steps.

  4. At the MATLAB command line, examine the constant_value_struct structure that the Constant block uses for its Constant value parameter.

    constant_value_struct
    
    constant_value_struct = 
    
        A: [1x1 struct]
        B: 5
        C: [1x1 struct]
  5. Examine the logged data in the logsout variable, focusing on the B element of the A1 bus signal. The constant_value_struct structure sets the B element to 5.

    logsout.A1.B.Data
    

Setting Configuration Parameters to Support Using a Bus Object Data Type

To enable the use of a bus object as an output data type, before you start a simulation, set the following diagnostics as indicated:

The documentation for these diagnostics explains how to convert your model to handle error messages the diagnostics generate.

Arrays of Buses Not Supported

You cannot use an array of buses with a Constant block.

Examples

The following Simulink examples show how to use the Constant block:

Characteristics

Direct Feedthrough

N/A

Sample Time

Specified in the Sample time parameter

Scalar Expansion

No

Dimensionalized

Yes

Multidimensionalized

Yes

Zero-Crossing Detection

No

Was this topic helpful?