Guy on Simulink

Simulink & Model-Based Design

Where is the Reverse Multi-port Switch Block?

I recently noticed requests for a block implementing a behavior that might be called "Reverse Multiport Switch". You can find a few request like that on MATLAB Answers here and here.

In my opinion, the difficulty for users to implement this behavior is in-part due to a problem with word selections. English not being my first language, I sometimes end up in situations where I am not sure which word to use for describing something. I think something similar is happening here.

What does the user really want to do? I think phrasing this need in a different way can help us find the best solution. What about: I want a block that takes one input signal, and assigns it's value to one out of N outputs.

Assignment Block

As hinted by the rephrasing above, the Assignment block is what you are looking for. How to use it depends on the behavior you want for the inactive outputs. Let's see a few options.

Option 1: Disabled paths output zero

To begin, drag an Assignment block from the Simulink Library Browser and set its Index Option to Index vector (port)

Index Option: Port

This will give you a block that takes one input and can assign it to one out of N outputs:

Assignment block, option 1

Option 2: Holding the last value externally

What if I want the inactive outputs to hold the last value?

Use a Unit Delay block, or a Memory block to hold the values. (Memory vs Unit Delay, which one do you use? That is a topic for another time.)

Assignment block, holding output using a Memory block

This options makes it clear that the values of the inactive outputs are stored to be reused at the next step.

Warning: Not an option

Here I need to warn you about a method which might seem attractive, but will likely lead to problems.

In the Assignment block, you have the option to set Initialize output (Y) to Specify size for each dimension in table.

Assignment block dialog, Specify size for each dimension in table

Using this option, the Y0 input port will disappear from the block and the block will only write to the selected piece of memory.

In some simple models, this option can give results identical to option 2 above. However I strongly recommend not using it. This option is designed to be used in Iterator Subsystems (For and While).

If you use this option, ensure that setting the Action if any output element is not assigned parameter to Error does not error out. This diagnostic is to protect against the condition where elements are not assigned, and your algorithm reads from uninitialized memory. During simulation memory is always initialized to zero, but in the generated code, this can be uninitialized and behavior will be non-deterministic.

Now it's your turn

Are there other blocks like the "Reverse Multi-Port Switch" you are looking for? Let us know by leaving a comment here.

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.