# Guy and Seth on Simulink

## The If-Else Construct in Models

Balaji Kalluri asked a question in the comments of a post about the MUX block.  Balaji asked:

Hi All
I would be highly obliged, if someone can show me a route to model a typical 2-to-1 Multiplexer, the way we have read & understood it. I want to translate behavior of typical digital 2-to-1 Mux works like this into SIMULINK model.

if(sel==0)
out = in1;
elseif (sel==1)
out = in2;

Today I want to look at one way to solve this question.

Controlling which signal passes through (Switch)

One way to think about this problem is to control which signal gets passed through the system.  The switch block provides a way to pass through one signal or another based on the value of a control signal.

This creates an if-expression: when the conditional statement is true, out is set equal to the first inport, otherwise, out is set equal to the third inport.

Optimizing the model

This represents the desired logic, but there is another way to think about the problem.  Consider that both in1 and in2 are the result of some other expensive calculations.  When this is the case, you will want to perform only one of the calculations needed for your output expression instead of always computing both values and then discarding the one that isn’t needed.

Luckily, this is a common pattern and Simulink provides an optimization to improve efficiency.  By default, Conditional input branch execution is turned On (in the Configuration Parameters -> Optimization page).

This optimization allows Simulink to group the upstream blocks together and tuck them into the conditional statement of the Switch block.  You may be able to understand the behavior if you turn on the block sorted order display, but I prefer to look at the code for simple models like this.

This optimization is not just limited to the Switch blocks.  Other blocks that produce conditional expressions can incorporate upstream and downstream blocks in their code.

I want to end this discussion with two important notes.

1. I made up this example specifically to demonstrate the effect of this optimization.  Because it is so simple, I had to disable Expression Folding, or else the code would be the same, no matter what.  I do not encourage anyone disable expression folding without good reason.
2. Because this is an optimization, you cannot guarantee this behavior.  If you add something as simple as a Test point between the Gain and Switch Block this will disable the optimization.

There are other ways to model efficient conditional behavior and explicitly guard the execution of an algorithm.  I will leave that topic for a future post.

Now it’s your turn

Do you think about the efficiency of your models?  How do you use the Switch block?  Leave a comment here and share what you know.

### 8 Responses to “The If-Else Construct in Models”

1. Anand Rangaramu replied on :

This is great. We need to implement this in our code.

2. Seth replied on :

@Anand Rangaramu – I hope you can turn on this optimization, but I think you should also strive to add explicit specifications of conditional behavior to your model. Stay tuned for a post on that.

3. Ramin replied on :

Can this be used to pass in a large structure, then after the first pass, not have the large structure as input?
Like a Data File which gets passed in once, and then not looked passed in after that?

Does this just impact the generated code, or does the Matlab/Simulink Internals fetch the data anyways before the assignments in the code? I’d like to have a selector “first_time_called” then not have the input get copied since it won’t change after the first call in generated code or simulink engine. (Note: values in the file are not known until run time so it can’t be hard coded)

4. HansUS replied on :

I wonder if the pseudo code on the right is correct.
Copy and paste mistake?

Without the Signal storage reuse option set,
the if-branch should use rtb_in1 instead of rtb_in2.

With activated Signal storage reuse option,
both if- and else-branch should directly write to the out variable.

5. Seth replied on :

@HansUS – Notice that both sides of the IF statement reference the same temporary variable, in this case named rtb_in2. I think you are suggesting that it would be more efficient to directly write to the out variable, and you are correct. The reason this model doesn’t do that is because I disabled the expression folding optimization. The model is so simple that with expression folding turned on, both codes were the same. Please see important note 1 above. Thanks for the comment.

6. Orcun replied on :

What if there is a unit delay or so in an input branch. Will the optimization work correct? Or just do the optimization for the part of the branch which is after the unit delay?

7. Seth Popinchalk replied on :

@Orucn – the Unit Delay represents a state in the system. States upstream of an switch block should not be optimized as the state still needs to update, no matter what the value of the switch. If you want it to execute differently, it would be better to explicitly model the conditional behavior. There is an example of this in the post titled A Better If-Else Construct.

8. Tabassum replied on :

oh God i got, wt i wanted…implementing a multiplexer

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 1 + 4 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Guy Rouleau and Seth Popinchalk are Application Engineers for MathWorks. They write here about Simulink and other MathWorks tools used in Model-Based Design.

These postings are the author's and don't necessarily represent the opinions of MathWorks.