Main Content

Constant

Constant value

  • Constant block

Libraries:
Simulink / Commonly Used Blocks
Simulink / Sources
DSP System Toolbox / Sources
HDL Coder / Commonly Used Blocks
HDL Coder / Sources

Alternative Configurations of Constant Block:
Eulers Number | Inf | NaN | Negative Inf | One | ...

Description

The Constant block generates a real or complex constant value signal. Use this block to provide a constant signal input. 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

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 check box. Otherwise, if you specify a vector for the Constant value parameter, the block treats that vector as a matrix.

Tip

To output a constant enumerated value, consider using the Enumerated Constant (Simulink) block instead. The Constant block provides block parameters that do not apply to enumerated types, such as Output minimum and Output maximum.

Using Bus Objects as the Output Data Type

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

Using Structures for the Constant Value of a Bus

The structure you specify must contain a value for every element of the bus represented by the bus object. The block output is a nonvirtual bus signal.

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

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

If the signal elements in the output bus use numeric data types other than double, you can specify the structure fields by using typed expressions such as uint16(37) or untyped expressions such as 37. To control the field data types, you can use the bus object as the data type of a Simulink.Parameter object. To decide whether to use typed or untyped expressions, see Control Data Types of Initial Condition Structure Fields (Simulink).

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 Configuration Parameters > Diagnostics > Data Validity > Advanced parameters > Underspecified initialization detection to Simplified. For more information, see Underspecified initialization detection (Simulink).

Examples

expand all

Open and simulate the model named SimpleGain.

mdl = "SimpleGain";
open_system(mdl)
sim(mdl);

A Gain block multiples input from a Constant block. A Display block displays the result.

This model represents the equation:

2*5=10

The Constant block provides a constant value of 2 to the Gain block.

The Gain block multiplies its input by 5.

The Display block displays the result of the multiplication.

Extended Examples

Ports

Output

expand all

Constant value, specified as a real or complex valued scalar, vector, matrix, or N-D array. By default, the Constant block outputs a signal whose dimensions, 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.

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

Parameters

expand all

Main

Specify the constant value output of the block.

The default value depends on the block you add.

  • 1Constant or One block

  • 0Zero block

  • infInf block

  • -infNegative Inf block

  • exp(1)Eulers Number block

  • piPi block

  • NaNNaN block

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

If you set Output data type to a bus object, you can specify one of these options:

  • 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 Using Bus Objects as the Output Data Type.

For nonbus data types, Simulink converts this parameter from its value data type to the specified output data type offline, using a rounding method of nearest and overflow action of saturate.

Dependencies

The software ignores the data type of the constant value when Output data type specifies a Simulink.ValueType object.

When this parameter specifies a Simulink.Parameter object and Output data type specifies a Simulink.ValueType object:

  • The block uses the minimum, maximum, and data type specified by the Simulink.ValueType object.

  • The block uses the unit specified by the Simulink.Parameter object.

  • The dimensions and complexity specified by the objects must match.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

Parameter: Value
Values: scalar in quotes | vector in quotes | matrix in quotes | array in quotes
Data Types: char | string

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.

  • When you select this check box, the block outputs a vector of length N, provided the Constant value parameter evaluates to an N-element row or column vector.

  • When you clear this check box, the block outputs a matrix of dimension 1-by-N or N-by-1, provided 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.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

Parameter: VectorParams1D
Values: 'on' (default) | 'off'

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

The default value of inf 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 (Simulink) for more information.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

Parameter: SampleTime
Values: 'inf' (default) | scalar in quotes | vector in quotes
Data Types: char | string

Signal Attributes

Specify the lower value of the output range that the software checks as a finite, real, double, scalar value.

The software uses the minimum to perform:

Tips

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

Dependencies

The software ignores the value of this parameter when Output data type specifies a Simulink.ValueType or Simulink.Bus object. The software uses the minimum values specified by the Simulink.ValueType object or the Simulink.BusElement objects in the Simulink.Bus object instead.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

Parameter: OutMin
Values: '[ ]' (default) | scalar in quotes

Specify the upper value of the output range that the software checks as a finite, real, double, scalar value.

The software uses the maximum value to perform:

Tips

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

Dependencies

The software ignores the value of this parameter when Output data type specifies a Simulink.ValueType or Simulink.Bus object. The software uses the maximum values specified by the Simulink.ValueType object or the Simulink.BusElement objects in the Simulink.Bus object instead.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

Parameter: OutMax
Values: '[ ]' (default) | scalar in quotes

Specify the output data type.

The Data Type Assistant helps you set data attributes. To use the Data Type Assistant, click the Show data type assistant button. For more information, see Specify Data Types Using Data Type Assistant (Simulink).

You can specify any of these options:

  • Inherited data type

  • Built-in Simulink data type — For example, specify single or uint8. See Data Types Supported by Simulink (Simulink).

  • Fixed-point data type — Use the fixdt (Simulink) function. For example, specify fixdt(1,16,0).

  • Enumerated data type — Use the name of the type preceded by Enum:. For example, specify Enum: myEnumType.

  • Bus data type — Use the name of the Simulink.Bus (Simulink) object preceded by Bus:. For example, specify Bus: myBusObject.

  • Value type (since R2023b) — Use the name of the Simulink.ValueType (Simulink) object preceded by ValueType:. For example, specify ValueType: windVelocity. The value type you specify must have DimensionsMode set to 'Fixed'.

  • Custom data type — Use a MATLAB expression that specifies the type. For example, you can specify a Simulink.NumericType (Simulink) object whose DataTypeMode property is set to a value other than 'Fixed-point: unspecified scaling'.

When you specify a Simulink.ValueType or Simulink.Bus object as the data type, the Output minimum and Output maximum parameters of the Constant block are ignored. The software uses the corresponding properties of the Simulink.ValueType object or the Simulink.BusElement objects in the Simulink.Bus object instead. For example, suppose a block sets Output minimum to 0. When the Output data type of the block specifies a ValueType object that has 1 as its minimum, the block uses 1 instead of 0 as its minimum.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

Parameter: OutDataTypeStr
Values: 'Inherit: Inherit from 'Constant value'' (default) | 'Inherit: Inherit via back propagation' | 'double' | 'single' | 'half' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'int64' | 'uint64' | 'boolean' | 'fixdt(1,16)' | 'fixdt(1,16,0)' | 'fixdt(1,16,2^0,0)' | 'Enum: <class name>' | 'Bus: <object name>' | 'ValueType: <object name>' | '<data type expression>'

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

To set the block parameter value programmatically, use the set_param (Simulink) function.

Parameter: LockScale
Values: 'off' (default) | 'on'

Select the category of data to specify.

  • Inherit — Inheritance rules for data types. Selecting Inherit enables a second menu to the right. Select one of these choices:

    • Inherit from 'Constant value' (default)

    • Inherit via back propagation

  • Built in — Built-in data types. Selecting Built in enables a second menu to the right. Select one of these choices:

    • double (default)

    • single

    • half

    • int8

    • uint8

    • int16

    • uint16

    • int32

    • uint32

    • int64

    • uint64

    • boolean

  • Fixed point — Fixed-point data types.

  • Enumerated — Enumerated data types. Selecting Enumerated enables a text box to the right, where you can enter a class name.

  • Bus object — Bus object. Selecting Bus object enables a text box to the right, where you enter the name of a bus object that you want to use to define the structure of the bus. To create or change a bus object, click Edit to the right of the text box.

  • Value type (since R2023b) — Value type. Selecting Value type enables a text box to the right, where you enter the name of a value type that you want to use to define the properties of the output signal. To create or change a value type, click Edit to the right of the text box.

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

    Do not specify a bus object as the expression.

Select the data type override mode for this signal.

  • When you select Inherit, the software inherits the data type override setting from its context, that is, from the block, Simulink.Signal object, or Stateflow® chart that is using the signal.

  • When you select Off, the software ignores the data type override setting of its context and uses the fixed-point data type specified for the signal.

For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Dependencies

To enable this parameter, click the Show data type assistant button, and set the Mode to Built in or Fixed point.

Tips

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.

Specify whether the fixed-point data is signed or unsigned. Signed data can represent positive and negative values, but unsigned data represents positive values only.

  • Signed, specifies the fixed-point data as signed.

  • Unsigned, specifies the fixed-point data as unsigned.

For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Dependencies

To enable this parameter, set the Mode to Fixed point.

Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors. For more information, see Specifying a Fixed-Point Data Type (Simulink).

Dependencies

To enable this parameter, set Mode to Fixed point.

Specify the bit size of the word that holds the quantized integer. For more information, see Specifying a Fixed-Point Data Type (Simulink).

Dependencies

To enable this parameter, set Mode to Fixed point.

Specify fraction length for fixed-point data type as a positive or negative integer. For more information, see Specifying a Fixed-Point Data Type (Simulink).

Dependencies

To enable this parameter, set Scaling to Binary point.

Specify slope for the fixed-point data type. For more information, see Specifying a Fixed-Point Data Type (Simulink).

Dependencies

To enable this parameter, set Scaling to Slope and bias.

Specify bias for the fixed-point data type as any real number. For more information, see Specifying a Fixed-Point Data Type (Simulink).

Dependencies

To enable this parameter, set Scaling to Slope and bias.

Block Characteristics

Data Types

Boolean | bus | double | enumerated | fixed point | half | integer | single

Direct Feedthrough

no

Multidimensional Signals

yes

Variable-Size Signals

no

Zero-Crossing Detection

no

Alternative Configurations

expand all

The Eulers Number block sets Constant value to exp(1). For more information, see exp.

Libraries:
Simulink / Quick Insert / Sources

The Inf block sets Constant value to inf. For more information, see Inf.

Libraries:
Simulink / Quick Insert / Sources

The NaN block sets Constant value to NaN. For more information, see NaN.

Libraries:
Simulink / Quick Insert / Sources

The Negative Inf block sets Constant value to -inf. For more information, see Inf.

Libraries:
Simulink / Quick Insert / Sources

The One block differs from the Constant block in name only.

Libraries:
Simulink / Quick Insert / Sources

The Pi block sets Constant value to pi. For more information, see pi.

Libraries:
Simulink / Quick Insert / Sources

The Zero block sets Constant value to 0.

Libraries:
Simulink / Quick Insert / Sources

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
Design and simulate fixed-point systems using Fixed-Point Designer™.

Version History

Introduced before R2006a

expand all