This model shows the following Simulink® concepts:
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 example 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 example, 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 Operator 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: You can display the data type of each signal by checking the Port Data Types command in the Simulink diagram's Display > Signals & Ports menu.
Boolean Signals in Simulink®
You have the option to have Simulink to use boolean or double values for the inputs and outputs of the Logical Operator block, using the following steps.
1. Open the Configuration Parameters dialog box by choosing the Configuration parameters command from the Simulation menu.
2. Go to the Optimization page in the Configuration Parameters dialog.
3. See that the Implement logic signals as boolean data (vs. double) parameter has been enabled. This optimization tells Simulink whether or not it should allow a signal whose data type is double to be passed into and out of blocks that support the boolean data type.
4. Click the OK button on the Configuration Parameters dialog.
By having the Data Type Conversion block after the Discrete Pulse Generator block, you avoid an error indicating that the Logical Operator block expects a boolean input signal.
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 Background Color
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 and MATLAB® code!
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. A Level-2 MATLAB-file S-function block, which runs the MATLAB 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.
For more information on how to write MATLAB file S-functions, see the documentation.