{"id":30,"date":"2006-03-29T12:44:01","date_gmt":"2006-03-29T17:44:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=30"},"modified":"2018-01-08T16:12:59","modified_gmt":"2018-01-08T21:12:59","slug":"understanding-persistence","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/03\/29\/understanding-persistence\/","title":{"rendered":"Understanding Persistence"},"content":{"rendered":"<style xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\">\r\n\r\npre.codeinput {\r\n  background: #F9F7F3;\r\n  padding: 10px;\r\n  border: 1px solid rgb(200,200,200);\r\n}\r\n\r\n@media print {\r\n  pre.codeinput {word-wrap:break-word; width:100%;}\r\n} \r\n\r\nspan.keyword {color: #0000FF}\r\nspan.comment {color: #228B22}\r\nspan.string {color: #A020F0}\r\nspan.untermstring {color: #B20000}\r\nspan.syscmd {color: #B28C00}\r\n\r\npre.codeoutput {\r\n  background: #F9F7F3;\r\n  color: #777777;\r\n  padding: 10px;\r\n  border: 1px solid rgb(200,200,200);\r\n}\r\n\r\npre.error {\r\n  color: red;\r\n}\r\n\r\np.footer {\r\n  text-align: right;\r\n  font-size: xx-small;\r\n  font-weight: lighter;\r\n  font-style: italic;\r\n  color: gray;\r\n}\r\n\r\n  <\/style><div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Questions about using <a>persistent variables<\/a> arise from time to time in the MATLAB newsgroup. Today I'd like to show you a little about what you might think you can do,\r\n         but can't with persistent variables.  I am also curious to <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=30#respond\">hear from you<\/a> about your use persistent variables, how often, and in\r\n         what contexts.  I personally now use nested functions exclusively (I think?) instead.  Here are some links: <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=21\">blog article [1]<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=19\">blog article [2]<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/nested-functions.html\">the documentation<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">First Sample Function<\/a><\/li>\r\n         <li><a href=\"#4\">Second Sample Function<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>First Sample Function<a name=\"1\"><\/a><\/h3>\r\n   <p>Here is the first of two sample functions that appear to be fairly similar. However, there are profound differences in the\r\n      these functions, and only the first one will run in MATLAB.\r\n   <\/p><pre class=\"codeinput\">type <span class=\"string\">fcnPersist1<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction q = fcnPersist1(u)\r\n% fcnPersistent creates and uses a persistent variable.\r\npersistent y;\r\nif isempty(y)\r\n    y = u;\r\nelse\r\n    y = y+1;\r\nend\r\nq = y;\r\n\r\n\r\n<\/pre><p>What you see is:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>first the declaration that the variable <tt>y<\/tt> is <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/persistent.html\"><tt>persistent<\/tt><\/a>, followed by\r\n         <\/li>\r\n         <li>some code to either initialize the persistent variable or to update its value, and<\/li>\r\n         <li>finally compute the desired output.<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>When I run <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mlint.html\"><tt>mlint<\/tt><\/a> on this file, I find no messages.  And when I run the code, I get no warning messages.\r\n   <\/p><pre class=\"codeinput\">fcnPersist1(0)\r\nfcnPersist1(3)\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n     4\r\n\r\n\r\nans =\r\n\r\n     5\r\n\r\n<\/pre><h3>Second Sample Function<a name=\"4\"><\/a><\/h3>\r\n   <p>Here's the second sample function.  In this function, you can see that I try to assign the persistent variable <tt>y<\/tt> to also be the output of the function.  This is not allowed, and <tt>mlint<\/tt> does warn about us about this.\r\n   <\/p><pre class=\"codeinput\">type <span class=\"string\">fcnPersist2<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction y = fcnPersist2(u)\r\n% fcnPersist2 tries to create and use a persistent variable.\r\npersistent y;\r\nif isempty(y)\r\n    y = u;\r\nelse\r\n    y = y+1;\r\nend\r\n\r\n<\/pre><p>Here's on line of the <tt>mlint<\/tt> output:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><tt>'y' appears to be incompatibly used or redefined.<\/tt><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>This redefinition of <tt>y<\/tt>, as both an output variable and as a persistent variable, is prohibited in MATLAB and the result, as in the first example,\r\n      must be assigned to another output variable.\r\n   <\/p>\r\n   <p class=\"footer\"><br>\r\n      Published with MATLAB&reg; 7.2<br><\/p>\r\n<\/div>\r\n<!--\r\n##### SOURCE BEGIN #####\r\n%% Persistent Usage\r\n% Questions about using <http:\/\/ persistent variables>\r\n% arise from time to time in the MATLAB newsgroup. Today I'd like to show\r\n% you a little about what you might think you can do, but can't with\r\n% persistent variables.  I am also curious to hear how many of you use\r\n% persistent variables, how often, and in what contexts.\r\n\r\n%% First Sample Function\r\n% Here is the first of two sample functions that appear to be fairly similar.\r\n% However, there are profound differences in the these functions, and only\r\n% the first one will run in MATLAB.\r\n\r\ntype fcnPersist1\r\n\r\n%%\r\n% What you see is:\r\n%\r\n% * first the declaration that the variable |y| is \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/persistent.html |persistent|>,\r\n% followed by \r\n% * some code to either initialize the persistent variable or to\r\n% update its value, and \r\n% * finally compute the desired output.\r\n%\r\n\r\n%%\r\n% When I run\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/mlint.html |mlint|>\r\n% on this file, I find no messages.  And when I run the code, I get no\r\n% warning messages.\r\nfcnPersist1(0)\r\nfcnPersist1(3)\r\n%% Second Sample Function\r\n% Here's the second sample function.  In this function, you can see that I\r\n% try to assign the persistent variable |y| to also be the output of the\r\n% function.  This is not allowed, and |mlint| does warn about us about this.\r\ntype fcnPersist2\r\n\r\n%%\r\n% Here's on line of the |mlint| output:\r\n% \r\n% * |'y' appears to be incompatibly used or redefined.|\r\n%\r\n%%\r\n% This redefinition of |y|, as both an output variable and as a persistent\r\n% variable, is prohibited in MATLAB and the result, as in the first\r\n% example, must be assigned to another output variable.\r\n\r\n\r\n\r\n\r\n##### SOURCE END #####\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\npre.codeinput {\r\n  background: #F9F7F3;\r\n  padding: 10px;\r\n  border: 1px solid rgb(200,200,200);\r\n}\r\n\r\n@media print {\r\n  pre.codeinput {word-wrap:break-word; width:100%;}\r\n} \r\n\r\nspan.keyword... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/03\/29\/understanding-persistence\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/30"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":2608,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/30\/revisions\/2608"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}