Seth on Simulink

June 2nd, 2009

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.

Model of conditional pass through using a switch.

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

Conditional Input Branch Execution optimization from Simulink configuration parameters. 

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.

Comparison of generated code with and without conditional input branch execution optimization.

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.

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

Leave a Reply

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


Seth Popinchalk is an Application Engineer for The MathWorks. He writes here about Simulink and other MathWorks tools used in Model-Based Design.
  • Guy: @Shahin - The Simulink Control Design toolbox offers functionalities similar to the one provided by the new PID...
  • Shahin Moghimi: Hi, I have the R2008a version, and am wondering if there is such an automatic tool in my version as...
  • ponmani: Respected sir, i am working simulink for the past 6 months. i am trying to place my control algorithm...
  • Seth: @Aleksandar - If you build a Simulink model into an executable using Real-Time Workshop, you will only be able...
  • Aleksandar: Seth, thank you very much! I have additional question. Is it possible to alter Simulink model built into...
  • Seth: I am not sure of your implementation of the D flip flop, and I am not sure how it is causing an algebraic loop...
  • esayas: nice work !
  • Seth: @Checker - Due to the high volume of reservation requests our career fair team had to pre-screen profiles based...
  • Checker: I filled it out couple of days back and got an emial saying that the no. of people they can handle is...
  • Seth: @Jonathan - in the case of the Buffer block, you can not use the result calculated in one part of the model to...

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