{"id":707,"date":"2012-04-09T13:27:11","date_gmt":"2012-04-09T18:27:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=707"},"modified":"2012-04-09T13:27:11","modified_gmt":"2012-04-09T18:27:11","slug":"conditional-execution","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2012\/04\/09\/conditional-execution\/","title":{"rendered":"Conditional Execution"},"content":{"rendered":"<p>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.<\/p>\r\n\r\n<p>I will share a few possible options I usually consider and would like to know how you choose one versus another.<\/p>\r\n\r\n<p><strong>Enabled Subsystem<\/strong><\/p>\r\n\r\n<p>Maybe it is because it is the first method I learned when I started using Simulink long ago, but I like <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/ug\/f4-84107.html\">Enabled Subsystems<\/a>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/enabledSubsystem.png\" alt=\"Enabled Subsystem\"><\/p>\r\n\r\n<p>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.<\/p>\r\n\r\n<p><strong>Switch and Multiport Switch<\/strong><\/p>\r\n\r\n<p>As explained by Seth in a <a href=\"https:\/\/blogs.mathworks.com\/seth\/2009\/06\/02\/the-if-else-construct-in-models\/\">previous post<\/a>, the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/slref\/switch.html\">Switch<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/slref\/multiportswitch.html\">Multiport Switch<\/a> blocks can be used to implement conditional execution.<\/p>\r\n\r\n<p>If you enable the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/gui\/bq7cqu2-1.html#bq7cq0i-1\">Conditional branch execution<\/a> optimization, Simulink executes only the blocks required to compute the control input and the data input selected by the control input. <\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/switch.png\" alt=\"Switch\"><\/p>\r\n\r\n<p>I typically use this method only when I need to switch between 2 very simple options, made of just a few directfeedthrough stateless blocks.<\/p>\r\n\r\n<p><strong>Action Subsystems<\/strong><\/p>\r\n\r\n<p><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/slref\/ifactionsubsystem.html\">If Action Subsystem<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/slref\/switchcaseactionsubsystem.html\">Switch Case Action Subsystem<\/a> are especially designed to model <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/slref\/if.html\">if-else<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/slref\/switchcase.html\">switch-case<\/a> control flows.<\/p>\r\n\r\n<p>In this approach, you define the conditions in the dialog of the If-Else or Switch-Case block.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/ifSubsystem.png\" alt=\"If Action Subsystem\"><\/p>\r\n\r\n<p>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.<\/p>\r\n\r\n<p><strong>Stateflow and Simulink Functions<\/strong><\/p>\r\n\r\n<p>For complex activation logic, I recommend going with Stateflow. Inside the chart, you can place the algorithms to be conditionally executed inside Simulink Functions.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/stateflowSLFunction.png\" alt=\"Simulink function inside Stateflow\"><\/p>\r\n\r\n<p>I like to use this technique especially when the Simulink function is called from multiple places in the chart.<\/p>\r\n\r\n<p><strong>Stafeflow and Function-Call Subsystems<\/strong><\/p>\r\n\r\n<p>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.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/sf_fcn_call.png\" alt=\"Stateflow and Function-Call Subsystems\"><\/p>\r\n\r\n<p><strong>Stafeflow, Enumeration and Switch-Case Action Subsystems<\/strong><\/p>\r\n\r\n<p>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.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/stateflowEnum.png\" alt=\"Stateflow and Function-Call Subsystems\"><\/p>\r\n\r\n<p><strong>Conclusion<\/strong><\/p>\r\n\r\n<p>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.<\/p>\r\n\r\n<p>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.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>I would be very interested to hear what are your criteria when choosing one versus another.<\/p>\r\n\r\n<p>Let us know by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=707&amp;#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>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.\r\n\r\nI will share... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2012\/04\/09\/conditional-execution\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,11,33,56],"tags":[262,455],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/707"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=707"}],"version-history":[{"count":24,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":796,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/707\/revisions\/796"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}