{"id":3953,"date":"2014-08-06T06:00:11","date_gmt":"2014-08-06T11:00:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=3953"},"modified":"2014-08-05T16:25:59","modified_gmt":"2014-08-05T21:25:59","slug":"how-do-you-hold-the-value-of-a-signal","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2014\/08\/06\/how-do-you-hold-the-value-of-a-signal\/","title":{"rendered":"How do you hold the value of a signal?"},"content":{"rendered":"<p>Yesterday, I was giving a training to new hires at MathWorks and one of the attendees asked me how to hold a value in Simulink.<\/p> \r\n\r\n<p>Of course, my answer was that there are many ways to hold a value in Simulink. I made him a few examples that I am sharing today in this post.<\/p>\r\n\r\n<p><strong>The Desired Behavior<\/strong><\/p>\r\n\r\n<p>I want to create a subsystem with two inputs, <tt>u<\/tt> and <tt>hold<\/tt>, and one output <tt>y<\/tt>. When <tt>hold<\/tt> is true, the output <tt>y<\/tt> is equal to the input <tt>u<\/tt>. When <tt>hold<\/tt> is zero, the output <tt>y<\/tt> remains constant, holding the last output value.<\/p>\r\n\r\n<p>Here are example input and output signals:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/hold_example.png\" alt=\"Example Signals\" \/><\/p>\r\n\r\n<p>Let's see a few ways to obtain such behavior<\/p>\r\n\r\n<p><strong>Method 1: Switch and Delay<\/strong><\/p>\r\n\r\n<p>The most common way to hold a value that I observe in customers models is using a <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/switch.html\">Switch<\/a> and a <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/unitdelay.html\">Unit Delay<\/a>, or <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/memory.html\">Memory<\/a> block<\/p>\r\n\r\n<p>Nice, clean and simple!<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/SwitchDelay.png\" alt=\"Holding a signal using a Switch and a Delay block\" \/><\/p>\r\n\r\n<p><strong>Method 2: Enabled Subsystem<\/strong><\/p>\r\n\r\n<p>Personally, my favorite way to hold a the value of a signal is using an <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/enabledsubsystem.html\">Enabled Subsystem<\/a>, with the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/outport.html\">Outport<\/a> property <tt>Output when disabled<\/tt> set to <strong>held<\/strong>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/enabledSS.png\" alt=\"Holding a signal using a enabled subsystem\" \/><\/p>\r\n\r\n<p>I like this method because it takes virtually no blocks.<\/p>\r\n\r\n<p><strong>Method 3: If Action Subsystems and Delay<\/strong><\/p>\r\n\r\n<p>This version is similar to the first method, but we replace the Switch by an arrangement of <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/ifactionsubsystem.html\">If Action Subsystems<\/a> and a <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/merge.html\">Merge<\/a> block.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/ifAction.png\" alt=\"Holding a signal using If Action Subsystems\" \/><\/p>\r\n\r\n<p>Personally, I don't use this syntax very often, mostly because it requires more blocks.<\/p>\r\n\r\n<p><strong>Method 4: Stateflow<\/strong><\/p>\r\n\r\n<p>Another easy way to obtain the behavior we are looking for is using <a href=\"https:\/\/www.mathworks.com\/products\/stateflow\/\">Stateflow<\/a><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/holdStateflow.png\" alt=\"Holding a signal using Stateflow\" \/><\/p>\r\n\r\n<p><strong>What about the generated code?<\/strong><\/p>\r\n\r\n<p>When choosing an implementation, many factors need to be taken into account. One of my favorite guidelines for was <a href=\"https:\/\/blogs.mathworks.com\/seth\/2011\/07\/25\/are-you-using-the-right-block\/?s_tid=Blog_Seth_Category#comment-1537\">suggested by a reader of this blog<\/a>: <strong>Clearly show the intended functionality<\/strong>. I will let you judge by yourself which implementation most clearly shows the intention of holding a signal.<\/p>\r\n\r\n<p>Another concern that many users have is: What does the code look like?<\/p>\r\n\r\n<p>In this case, all the methods generate similar code. to make the code easier to read for this post, I set the storage class of the three signals or interest (<tt>hold<\/tt>, <tt>u<\/tt>, <tt>y<\/tt>) to ExportedGlobal.<\/p>\r\n\r\n<p>Switch and Delay:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/switchCode.png\" alt=\"Code for Holding a signal\" \/><\/p>\r\n\r\n<p><em>***Note: To obtain the above code, I enabled inline parameters from the model configuration. Otherwise the threshold of the switch is a tunable parameter.<\/em><\/p>\r\n\r\n<p>Enabled Subsystem:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/enabCode.png\" alt=\"Code for Holding a signal\" \/><\/p>\r\n\r\n<p>If Action Subsystem:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/ifCode.png\" alt=\"Code for Holding a signal\" \/><\/p>\r\n\r\n<p><em>***Note: To obtain the above code, I manually set the Function Packaging option of the If Action Subsystem to Inline. Otherwise, since the two subsystems are identical, the coder generates a reusable function.<\/em><\/p>\r\n\r\n<p>Stafelow:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/sfCode.png\" alt=\"Code for Holding a signal\" \/><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>What is your preferred method to hold the value of a signal? Are there other functionalities for which you consider multiple implementations? Let us know by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=3953&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/sfCode.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Yesterday, I was giving a training to new hires at MathWorks and one of the attendees asked me how to hold a value in Simulink. \r\n\r\nOf course, my answer was that there are many ways to hold a value... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2014\/08\/06\/how-do-you-hold-the-value-of-a-signal\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,11,33,56],"tags":[398],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3953"}],"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=3953"}],"version-history":[{"count":34,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3953\/revisions"}],"predecessor-version":[{"id":4039,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3953\/revisions\/4039"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=3953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=3953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=3953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}