mergedemo.mdl Open this Model

Simulink Merge Block Demonstration

This interactive demonstration introduces you to the following Simulink concepts.

If you have not opened or have made changes to the Merge Block Demonstration Simulink model, please reinitialize the Simulink model mergedemo.mdl.


Model description

The left side of this model contains two blocks, for example the Repeating Sequence block, from the Simulink Sources library.  Each block generates a scalar output signal of the form displayed on the block's icon. These two signals are fed into different Enabled Subsystems, which are outlined with thick lines. The enabled (or conditionally executed) subsystems are controlled by the signal generated by a Discrete Pulse Generator block. This block's output toggles between the values of zero and one every five seconds. When the output of the Discrete Pulse Generator block is positive, the top enabled subsystem is simulating. When the output of the Discrete Pulse Generator block is zero, the bottom enabled subsystem is simulating. To force the bottom enabled subsystem to simulate when the Discrete Pulse Generator block's output is zero, the output is passed through a Logical Operator block set to the NOT operator. The NOT block outputs a value of one when its input is zero.

The outputs of the two enabled subsystems are fed into a Merge block. The output of the Merge block is, finally, displayed on a Simulink Scope.

You can run the simulation to see the output of the Merge block on the Scope. Notice that the type of waveform shown on the Scope changes every five seconds. This is due to the behavior of the Merge block.


Conditionally executed subsystems

The Merge block demonstration contains two conditionally executed subsystems. A conditionally executed subsystem is a subsystem that is only executed at particular times during the simulation, i.e., when certain conditions are met. In this case, the subsystems are enabled subsystems, distinguished by their icons and the additional input port on the top or bottom of the block, as shown below.

The signal feeding into the additional port is called the control signal. The value of this signal provides the condition that determines whether the subsystem is executed, or not, at any given time step. Enabled subsystems are executed when the control signal is strictly positive.

In this demonstration, you want to execute one of the enabled subsystems when the control signal is positive, and the other when the control signal is zero. You can do this by using the Logical Operator block in Simulink.


Logical Operators block

The Logical Operator block in Simulink gives you a method for incorporating logical operators and boolean signals into your Simulink diagram. You can open the Logical Operator block to see the different operators that are listed in the drop down menu. The current setting of NOT returns 1 (or TRUE) when the input signal is non-zero, or 0 (FALSE) when the input signal is strictly zero.

In this example, you can use this feature to generate a positive value when the control signal goes to zero by maintaining the current operator setting of NOT and closing the dialog.

Note: Even though you are using a Logical Operator block, the output of the block is not a boolean in Simulink. By default, all signals in Simulink are of the data type double. To see this, you can display the data type of each signal by checking the Port data types command in the Simulink diagram's Format menu.


Boolean signals in Simulink

You can force Simulink to use boolean values for the inputs and outputs of the Logical Operator block, using the following steps.

  1. Open the Simulation Parameters window by choosing the Simulation parameters... command from the Simulation menu.
  2. Go to the Advanced tab in the Simulation Parameters dialog.
  3. From the list of optimizations at the bottom of this panel, highlight the Boolean logic signals item. This optimization tells Simulink whether it should allow a signal whose data type is double to be passed into and out of blocks that support the boolean data type, or not.
  4. Select the On radio button, to turn the Boolean logic signals on. Now, Simulink will error out when it detects that any data type other than boolean is passed into a block expecting a logical signal.
  5. Press the OK button on the Simulation Parameters window.

If you run the simulation, you'll see an error indicating that the Logical Operator block expects a boolean input signal. While the error dialog is open, notice that the input and output data type of the NOT block is labeled boolean on the Simulink diagram.

You can use the following steps to remedy this error.

  1. Place a Data Type Conversion block after the Discrete Pulse Generator block.
  2. Open the Data Type Conversion block's Block Parameter dialog.
  3. Choose "boolean" from the Data type pull down menu and press the OK button.
  4. Run the simulation. It now contains boolean data types at the input and output of the Logical Operator block.

The Merge block and alternately enabling subsystems

You can use the Merge block to create a single signal whose value is equal to the output of whichever enabled subsystem is currently executing. The following steps.

  1. Open the Merge block's Block Parameter dialog by double clicking on the block.
  2. Specify the Number of inputs as the number of enabled subsystems whose outputs you want to merge. In this case you want to set the Number of inputs to be 2.
  3. Leave the Initial output field empty to specify that the Merge block sets its initial output to the initial value of one of its input signals. In this case, the output will be the initial value of the subsystem that is enabled when the simulation starts.
  4. Close the Merge block's Block Parameter dialog.
  5. Connect the output of the two conditionally executed subsystems to the inputs of the Merge block.

As before, when you run the simulation, the output of the Merge block is the value of whatever conditionally executed subsystem is executing at any point in time.


Interesting feature: Programmatically changing a block's BackgroundColor

When you ran the simulation, you should have noticed that the color of the currently executing conditionally executing subsystems changed. This does not happen automatically for all conditionally executing subsystems. It was achieved using an S-function!

If you open one of the enabled subsystems by double clicking on the block in the Simulink diagram, you'll see it contains the following.

  1. An Enable block, which is what makes this a conditionally executed subsystem and adds the input port for the control signal
  2. An S-function block, which runs the M-file S-function "mergefcn.m"

At each time step, the S-function's mdlUpdate subfunction executes. It sets the BackgroundColor property of the Subsystems based on whether it is currently executing, or not, with the following command.

set_param('mergedemo/Subsystem','BackgroundColor','green')

For more information on how to write M-file S-functions, see the documentation.

Close the Merge block demonstration Simulink diagram.