{"id":11182,"date":"2021-12-10T15:18:59","date_gmt":"2021-12-10T20:18:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=11182"},"modified":"2021-12-10T15:18:59","modified_gmt":"2021-12-10T20:18:59","slug":"subsystem-reference-and-test-harness","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2021\/12\/10\/subsystem-reference-and-test-harness\/","title":{"rendered":"Subsystem Reference and Test Harness"},"content":{"rendered":"<div class = rtcContent><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>In case you missed it, some time ago I wrote <\/span><a href = \"https:\/\/blogs.mathworks.com\/simulink\/2019\/11\/15\/new-in-r2019b-subsystem-reference\"><span>this post<\/span><\/a><span> introducing the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2021b\/simulink\/ug\/referenced-subsystem-1.html\"><span>Subsystem Reference<\/span><\/a><span>. I really like this feature and the way it allows you to simply store the contents of a Subsystem in a separate file.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Since its initial release in R2019b, a lot of enhancements have been made to the Subsystem Reference and today I want to highlight one I find particularly useful: <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2021b\/sltest\/gs\/create-a-test-harness.html\"><span>Test Harnesses<\/span><\/a><span>.<\/span><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>The Challenge<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>One recommendation I always give to users developing Simulink models is to try keeping the model in an \"updatable\" state as much as possible. This mostly means doing <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2021b\/simulink\/ug\/updating-a-block-diagram.html\"><span>update diagram<\/span><\/a><span> often (Shortcut: <\/span><span style=' font-weight: bold;'>Ctrl+D<\/span><span>). That way, if you make a mistake, you will quickly get an error describing what is wrong. Without that, if you make multiple changes before trying to update the model, the error messages might be more difficult to debug because there can be a combination of multiple inter-related issues.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>A Reference Subsystem can be edited in two ways, and both have caveats regarding the above suggestion:<\/span><\/div><ul  style = 'margin: 10px 0px 20px; padding-left: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px; '><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span style=' font-weight: bold;'>Editing standalone:<\/span><span> Since a Subsystem file does not have a complete configuration set, it is not possible to perform an update diagram on a standalone subsystem.<\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span style=' font-weight: bold;'>Editing an instance in a model:<\/span><span> In a model, it is possible to edit a Subsystem and hit Ctrl+D to validate your changes. One possible challenge with that approach is that, if the model is large and contains many blocks around the modified Subsystem, performing an update diagram might take time.<\/span><\/li><\/ul><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Let's see how a test harness can help with that.<\/span><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Creating a Default Test Harness<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Starting in MATLAB R2020b, the Subsystem tab of a Subsystem Reference file has a new Test Harness section allowing you to create test harnesses:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/SSRefTestHarness_1.png\" width = \"664\" height = \"205\" alt = \"\" style = \"vertical-align: baseline; width: 664px; height: 205px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>I am not going to describe in this post why it is always a good idea to create test harnesses for models and subsystems, you can visit the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2021b\/sltest\/gs\/create-a-test-harness.html\"><span>documentation page introducing test harnesses<\/span><\/a><span> to read more on that topic.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>What I want to point out in this post is that, with a test harness, we now have a context in which the subsystem can be simulated.<\/span><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Update With Test Harness<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Once a Test Harness has been added to a Subsystem file, the <\/span><span style=' font-weight: bold;'>Update With Test Harness<\/span><span> action becomes available and is associated with the same shortcut as in a model (<\/span><span style=' font-weight: bold;'>Ctrl+D<\/span><span>). <\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>This means that when you edit the Subsystem standalone, you will be able to frequently hit Ctrl+D and keep the subsystem in an updatable state. Under the hood, we will accomplish the update diagram using the test harness, so if there are no errors, you don't have to explicitly open the test harness.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>If there is an error, it will be reported in the Diagnostics Viewer with hyperlinks to the problematic blocks in the context of the harness.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/SSRefTestHarness_2.png\" width = \"813\" height = \"445\" alt = \"\" style = \"vertical-align: baseline; width: 813px; height: 445px;\"><\/img><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Simulate With Test Harness<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>In a similar manner, we also added a \"<\/span><span style=' font-weight: bold;'>Run with Test Harness<\/span><span>\" button to the toolstrip. Using this button, the Subsystem is simulated with the default harness, and you can visualize the logged data in the Simulation Data Inspector.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/SSRefTestHarness_3.png\" width = \"798\" height = \"529\" alt = \"\" style = \"vertical-align: baseline; width: 798px; height: 529px;\"><\/img><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Now it's your turn<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Are you leveraging test harnesses with Subsystem Reference? If yes, are you taking advantage of those to ensure that your Subsystem remains \"updatable\" and validate simulation results in a default test harness?<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Give this feature a try and let us know what you think in the comments below.<\/span><\/div>\r\n<\/div><script type=\"text\/javascript\">var css = ''; var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style'); head.appendChild(style); style.type = 'text\/css'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); }<\/script><a href=\"https:\/\/blogs.mathworks.com\/simulink\/files\/SSRefTestHarness.mlx\"><button class=\"btn btn-sm btn_color_blue pull-right add_margin_10\">Download Live Script<\/button><\/a>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/SSRefTestHarness_1.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>In case you missed it, some time ago I wrote this post introducing the Subsystem Reference. I really like this feature and the way it allows you to simply store the contents of a Subsystem in a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2021\/12\/10\/subsystem-reference-and-test-harness\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":11170,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[545,63,16],"tags":[622,424],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/11182"}],"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=11182"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/11182\/revisions"}],"predecessor-version":[{"id":11188,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/11182\/revisions\/11188"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media\/11170"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=11182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=11182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=11182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}