Documentation

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

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

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.

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.

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 Control Signal 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 Control Signal 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.

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.

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 decide whether to use typed or untyped expressions, see Decide Whether to Specify Data Types for Structure Fields.

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 ex_busic model and update it.

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

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

  3. Simulate the ex_constantbus 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
    

Group Constant Signals into an Array of Buses

You can use a Constant block to compactly represent multiple constant-valued signals as an array of buses. You can use this technique to reduce the number of signal lines in a model and the number of variables that the model uses, especially when the model repeats an algorithm with different parameter values.

To generate a constant-valued array of bus signals, use an array of MATLAB structures in a Constant block. The block output is an array of buses, and each field in the array of structures provides the simulation value for the corresponding signal element.

You can also use an array of structures to specify the Value property of a Simulink.Parameter object, and use the parameter object in a Constant block.

  1. Open the example model ex_constantbus_array.

    The variables ParamBus and const_param_struct appear in the base workspace. The variable const_param_struct contains a structure whose field names match the elements of the bus type that ParamBus defines.

  2. Update the diagram to view the signal line widths.

    The output of the Constant block is a single scalar bus of type ParamBus. The structure variable const_param_struct specifies the constant value in the block.

  3. At the command prompt, create an array of two structures by copying the structure const_param_struct. Customize the field values by indexing into the individual structures in the array.

    const_struct_array =...
     [const_param_struct const_param_struct];
    
    const_struct_array(2).Offset = 158;
    const_struct_array(2).Gain = 3.83;
    const_struct_array(2).Threshold = 1039.77
    
    const_struct_array = 
    
    1x2 struct array with fields:
    
        Offset
        Gain
        Threshold

  4. In the Constant block dialog box, set Constant value to const_struct_array.

  5. Add two Selector blocks to the model, and connect the Constant block as shown.

  6. In the Selector block dialog box, set Index to 1 and Input port size to 2.

  7. In the Selector1 block dialog box, set Index to 2 and Input port size to 2.

  8. Copy the block algorithm in the model, and connect the blocks as shown.

  9. Update the diagram. The signal line width and style indicate that the output of the Constant block is an array of buses. The Selector blocks each extract one of the buses in the array.

    Each copy of the algorithm uses the constant values provided by the corresponding structure in the variable const_struct_array.

To create an array of structures for a bus that uses a large hierarchy of signal elements, consider using the function Simulink.Bus.createMATLABStruct. You can also use this technique to create an array of structures if you do not have a scalar structure that you can copy.

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.

Examples

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

Related Examples

Characteristics

Data Types

Double | Single | Boolean | Base Integer | Fixed-Point | Enumerated | Bus

Sample Time

Specified in the Sample time parameter

Direct Feedthrough

N/A

Multidimensional Signals

Yes

Variable-Size Signals

No

Zero-Crossing Detection

No

Code Generation

Yes

Introduced before R2006a

Was this topic helpful?