{"id":8584,"date":"2019-02-19T15:22:41","date_gmt":"2019-02-19T20:22:41","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=8584"},"modified":"2019-02-21T13:56:04","modified_gmt":"2019-02-21T18:56:04","slug":"variants-what-are-your-options-in-r2018b","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2019\/02\/19\/variants-what-are-your-options-in-r2018b\/","title":{"rendered":"Variants: What Are Your Options in R2018b?"},"content":{"rendered":"<p>I posted about <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/variant-systems.html\">variants<\/a> in the past, but things have changed a lot since then, so I thought it would be a good idea to review the options available for variants in R2018b.<\/p>\n<p><strong>Variant Subsystem<\/strong><\/p>\n<p>If you are not familiar with the concept of variants, I recommend getting started with the documentation page <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/ug\/what-is-a-variant.html\">What Are Variants and When to Use Them<\/a>. In short, the concept of variants allows you to have blocks and subsystems that are present in the model, but conditionally compiled.<\/p>\n<p>For the Variant Subsystem, it can contain a set of variant implementations and only one of them can be active at a time. Here is an image giving an overview of the Variant Subsystem.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/whatsVariant.png\" alt=\"Variant Subsystem\" \/><\/p>\n<p><strong>Variant Sink and Source<\/strong><\/p>\n<p>Variant <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/slref\/variantsource.html\">Source<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/slref\/variantsink.html\">Sink<\/a> blocks are blocks with either multiple inputs and one output (Source) or one input and multiple outputs (Sink). Depending on different conditions that I will discuss later, only one port is active and blocks connected to the other ports are not included during model compilation.<\/p>\n<p>In the following example, the first input is active. The Sine Wave, the \"sin\" and the square root are inactive and grayed out because they are not needed.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/variantSource.png\" alt=\"variant source\" \/><\/p>\n<p>Now, let's see the options available to control which variant is active.<\/p>\n<p><strong>Controlling Variants using Label Mode<\/strong><\/p>\n<p>Previously named \"<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018a\/simulink\/slref\/variantsubsystem.html#d119e232249\">Override variant condition<\/a>\", the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/variantsubsystem.html#mw_acbaad91-8bfd-474b-baea-cfe614e470e4\">Label mode<\/a> is mostly targeting users who want to specify the active variants interactively.<\/p>\n<p>When you open the block parameters dialog of a variant (Subsystem, Source, or Sink -- they all work the same), you can configure the variant using these steps:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/labelMode.png\" alt=\"Configuring Label Mode\" \/><\/p>\n<p>Once the block is configured, you can change the active variant by right-clicking on the variant badge displayed on the block:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/labelMode2.png\" alt=\"Configuring Label Mode\" \/><\/p>\n<p>If you prefer, you can also use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/slref\/set_param.html\">set_param<\/a> to change the active variant programmatically.<\/p>\n<p><strong>Controlling Variants using Expression Mode<\/strong><\/p>\n<p>With Expression Mode, you specify an expression for each variant. This expression must be a valid MATLAB expression that evaluates to true or false.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/ExpressionMode.png\" alt=\"Configuring Expression Mode\" \/><\/p>\n<p><strong>Code Generation<\/strong><\/p>\n<p>When variants are controlled by an expression, you have additional capabilities related to code generation. The most important one is to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/ecoder\/ug\/generate-preprocessor-conditionals-for-variant-systems.html\">generate preprocessor conditional<\/a>.<\/p>\n<p>In the next image, I specified \"a==1\" and \"a==2\" as conditions in both the Variant Source and Sink blocks and I defined \"a\" as a Simulink.Parameter object in the base workspace. When generating code, both conditions are present and protected by preprocessor conditionals. This allows you to generate code only once and then build the different configurations.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/gpc.png\" alt=\"generate preprocessor conditional\" \/><\/p>\n<p><strong>Manual Variant Sink and Source<\/strong><\/p>\n<p>One last item to highlight: the Manual Variant <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016b\/simulink\/slref\/manualvariantsource.html (link no longer works)\">Source<\/a> and <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016b\/simulink\/slref\/manualvariantsink.html (link no longer works)\">Sink<\/a> blocks.<\/p>\n<p>Usage of these blocks is pretty straightforward: Double-click on the block to toggle which branch is active.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/ManualVariant.gif\" alt=\"Manual Variant\" \/><\/p>\n<p>During model compilation, only blocks connected to the active branch will be compiled.<\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>Let us know what you think of these options and how you control variants in your models.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/ManualVariant.gif\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>I posted about variants in the past, but things have changed a lot since then, so I thought it would be a good idea to review the options available for variants in R2018b.<br \/>\nVariant Subsystem<br \/>\nIf you... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2019\/02\/19\/variants-what-are-your-options-in-r2018b\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,33],"tags":[560],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8584"}],"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=8584"}],"version-history":[{"count":26,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8584\/revisions"}],"predecessor-version":[{"id":8672,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8584\/revisions\/8672"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=8584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=8584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=8584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}