%% Merging Signals
% 
% This example shows how to use conditionally executed subsystems and the
% <docid:simulink_ref#f7-891445 Merge> block to combine two inputs into a
% single output. The Merge block output value at any time is equal to the
% most recently computed output of its driving blocks.
%
% Use Merge blocks to interleave input signals that update at different
% times into a combined signal in which the interleaved values retain their
% separate identities and times. To combine signals that update at the same
% time into an array or matrix signal, use a <docid:simulink_ref#bqnmm4m
% Vector Concatenate or Matrix Concatenate> block. 
% 
% Copyright 2005-2021 The MathWorks, Inc.

%% 
% 
open_system('mergedemo');

%%
close_system('mergedemo',0); 

%%
% The left side of the model contains two blocks
% <docid:simulink_ref#f7-893467 Repeating Sequence> blocks from the
% Simulink Sources library.  Each block generates a scalar output signal of
% the form displayed on the block icon. 

%%
open_system('mergedemo')
hilite_system(['mergedemo/Repeating',char(10),'Sequence'],'find')

%%
close_system('mergedemo',0);

%%
% These two signals are input into different <docid:simulink_ref#bvn7cta-1
% Enabled Subsystem> blocks, which are outlined with thick lines.

%%
open_system('mergedemo')
hilite_system('mergedemo/Subsystem','find')
hilite_system('mergedemo/Subsystem1','find')

%%
close_system('mergedemo',0);

%%
% A signal generated by the <docid:simulink_ref#f7-891920 Discrete Pulse
% Generator> block controls the enabled (or conditionally executed)
% subsystems.

%%
open_system('mergedemo')
hilite_system(['mergedemo/Discrete',char(32),'Pulse',char(10),'Generator'],'find')

%%
close_system('mergedemo',0);

%%
% This block 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 output is zero, the output is passed through a
% <docid:simulink_ref#f7-1061608 Logical Operator> block set to the NOT
% operator. The NOT block outputs a value of one when its input is zero.

%%
open_system('mergedemo')
hilite_system(['mergedemo/Logical',char(10),'Operator'],'find')

%%
close_system('mergedemo',0);

%%
% The outputs of the two enabled subsystems are input into a Merge block. 

%%
open_system('mergedemo')
hilite_system('mergedemo/Merge','find')

%%
close_system('mergedemo',0);

%%
% A <docid:simulink_ref#f5-1117037 Scope> block displays the Merge block output.

%%
open_system('mergedemo')
hilite_system('mergedemo/Scope','find')

%%
% 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. Due to the behavior of the Merge block, the type of waveform
% shown on the Scope changes every five seconds.

%%
open_system('mergedemo/Scope')
sim('mergedemo');

%%
close_system('mergedemo/Scope');
close_system('mergedemo',0);

%% Conditionally Executed Subsystems
%
% This Merge block example contains two conditionally executed subsystems.

%%
open_system('mergedemo')
hilite_system('mergedemo/Subsystem','find')
hilite_system('mergedemo/Subsystem1','find')

%%
close_system('mergedemo',0);

%%
% A conditionally executed subsystem is a subsystem that is
% only executed at times during the simulation 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.

%%
% <<../EnabledSubsystem.png>>

%% 
% 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.

%% Logical Operator Block
%
% The <docid:simulink_ref#f7-1061608 Logical Operator> block provides a
% method for incorporating logical operators and boolean signals into your
% model. 

%%
open_system('mergedemo')
hilite_system(['mergedemo/Logical',char(10),'Operator'],'find')

%%
close_system('mergedemo',0);

%%
% Open the Logical Operator block to see the different types of operators.
% 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, 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:* In the Simulink Editor, on the *Debug* tab, the *Information
% Overlays* menu offers options for displaying signal and port properties
% on the block diagram.

%%
open_system('mergedemo')
set_param('mergedemo','ShowPortDataTypes','on')
set_param('mergedemo','SimulationCommand','update');
        
%%
close_system('mergedemo',0);

%% Boolean Signals
%
% You can have Simulink use boolean or double values for
% the inputs and outputs of the Logical Operator block, using the following
% steps.
%
% 1. To open the Configuration Parameters dialog box, in the Simulink
% Editor, on the *Modeling* tab, select *Model Settings*.
%
% 2. Go to the Optimization page in the Configuration Parameters dialog.
%
% 3. Verify that the *Implement logic signals as boolean data (vs. double)*
% parameter is 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*.
%

%%
% Placing the <docid:simulink_ref#f6-842904 Data Type Conversion> block
% after the Discrete Pulse Generator block avoids an error indicating
% that the Logical Operator block expects a boolean input signal. 

%%
open_system('mergedemo')
hilite_system('mergedemo/Data Type Conversion','find')

%%
close_system('mergedemo',0); 

%% Merge Block and Alternately Enabling Subsystems
%
% Use the Merge block to create a single signal that is equal to the
% output of whichever enabled subsystem is currently executing.

open_system('mergedemo')
hilite_system('mergedemo/Merge','find')

%%
close_system('mergedemo',0); 

%%
% 1. Open the Merge block dialog box by double-clicking
% 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 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 dialog box. 
%
% 5. Connect the output of the two conditionally executed subsystems
% to the inputs of the Merge block.

%%
open_system('mergedemo')
set_param('mergedemo/Merge','Inputs','2');
set_param('mergedemo/Merge','InitialOutput','[]');
PH=get_param({'mergedemo/Subsystem';'mergedemo/Subsystem1'},'PortHandles');
PH=[PH{:}];
Outs=[PH.Outport];
hilite_system(Outs);

%%
hilite_system(Outs,'none');

%%
% 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.

%%
open_system('mergedemo/Scope')
sim('mergedemo');
%%
close_system('mergedemo/Scope');
%% Programmatically Change Block Background Color
%
% When you run the simulation, the color of the currently executing
% conditionally executing subsystems change. This does *not* happen
% automatically for all conditionally executing subsystems. This example
% uses an S-function and MATLAB(R) code to change the block background
% color.
% 
% To see the code, double-click the one of the enabled subsystems to open
% it.

%%
% 1. The Enable block makes this a conditionally executed
% subsystem and adds the input port for the control signal.
open_system('mergedemo');
hilite_system('mergedemo/Subsystem/Enable','find')

%%
close_system('mergedemo/Subsystem',0);

%%
% 2. The Level-2 MATLAB-file S-function block runs the MATLAB file S-function
% <matlab:edit('mergefcn.m') mergefcn.m>.
open_system('mergedemo/Subsystem');
hilite_system('mergedemo/Subsystem/Enable','none')
hilite_system(['mergedemo/Subsystem/Level-2 MATLAB-file' char(10) 'S-Function'],'find')

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

%%
% <matlab:open_system('mergedemo');set_param('mergedemo/Subsystem','BackgroundColor','green') set_param('mergedemo/Subsystem','BackgroundColor','green')>
 
%%
close_system('mergedemo',0);