As we all know, in Simulink there are always multiple ways to implement a functionality. One situation where I often hesitate when choosing an implementation is conditional execution.
I will share a few possible options I usually consider and would like to know how you choose one versus another.
Maybe it is because it is the first method I learned when I started using Simulink long ago, but I like Enabled Subsystems.
Using this method, you use Simulink blocks to generate one logical signal for each subsystem. This method is useful when you have a small number of mutually exclusive systems and the activation logic is relatively simple.
Switch and Multiport Switch
As explained by Seth in a previous post, the Switch and Multiport Switch blocks can be used to implement conditional execution.
If you enable the Conditional branch execution optimization, Simulink executes only the blocks required to compute the control input and the data input selected by the control input.
I typically use this method only when I need to switch between 2 very simple options, made of just a few directfeedthrough stateless blocks.
If Action Subsystem and Switch Case Action Subsystem are especially designed to model if-else and switch-case control flows.
In this approach, you define the conditions in the dialog of the If-Else or Switch-Case block.
One thing to note with this approach is that all blocks in an Action subsystem driven by an If or Switch Case block must run at the same rate as the driving block.
Stateflow and Simulink Functions
For complex activation logic, I recommend going with Stateflow. Inside the chart, you can place the algorithms to be conditionally executed inside Simulink Functions.
I like to use this technique especially when the Simulink function is called from multiple places in the chart.
Stafeflow and Function-Call Subsystems
I have to admit, when I place a large or important algorithm inside a Stateflow Simulink Function, I feel like if I am hiding it. This is why sometimes I prefer to generate Function-Calls from the chart.
Stafeflow, Enumeration and Switch-Case Action Subsystems
One way to make it obvious that one and only one subsystem is active at a time is to generate an enumerated type from the Stateflow chart and use it in a Switch-Case construct.
All these methods have advantages and disadvantages. I think it is important to read and understand the documentation to pick the one appropriate for your use case.
When in doubt, go for readability. Try picking one that you think will make it as easy as possible to understand the functionality of your model.
Now it's your turn
I would be very interested to hear what are your criteria when choosing one versus another.
Let us know by leaving a comment here.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.