{"id":2205,"date":"2008-04-18T13:15:45","date_gmt":"2008-04-18T18:15:45","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2008\/04\/18\/renaming-variables\/"},"modified":"2016-11-16T15:19:50","modified_gmt":"2016-11-16T20:19:50","slug":"renaming-variables","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2008\/04\/18\/renaming-variables\/","title":{"rendered":"Renaming Variables"},"content":{"rendered":"<p><div class=\"alert alert-info\"> <span class=\"alert_icon icon-alert-info-reverse\"><\/span><p class=\"alert_heading\"><strong>Note<\/strong><\/p><p>The file submission referenced in this post is no longer available on File Exchange.<\/p><\/div><\/p>\n<p>Jiro&#8217;s pick this week is RENVAR by Duane Hanselman.<\/p>\n<p>&nbsp;<\/p>\n<p>Imagine that you defined a variable:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">oldVar = magic(3);<\/pre>\n<p>Now you want to rename this variable because the meaning of it has changed. How do you do it? Do you do it interactively from<br \/>\nthe Workspace Browser?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/renamevars_workspace.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>Or perhaps programmatically:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">newVar = oldVar;\r\nclear <span style=\"color: #a020f0;\">oldVar<\/span><\/pre>\n<p>Duane&#8217;s <tt>renvar<\/tt> let&#8217;s you do this in a single swoop.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">renvar <span style=\"color: #a020f0;\">newVar<\/span> <span style=\"color: #a020f0;\">veryNewVar<\/span><\/pre>\n<p>If you look inside his function, it is basically executing the two lines from above. I like this for a couple of reasons:<\/p>\n<div>\n<ul>\n<li>One line is shorter than two.<\/li>\n<li>It has a nice nugget of information regarding memory allocation.<\/li>\n<\/ul>\n<\/div>\n<p>From the H1 line:<\/p>\n<pre>  RENVAR Rename Variable Without Memory Reallocation.<\/pre>\n<p>Yes, in fact this method (as well as the two-liner) does not reallocate memory. No temporary memory is used, because it is<br \/>\ndoing &#8220;lazy copying&#8221;. So there&#8217;s no need to worry even if you are renaming a very large variable. Take a look at Loren&#8217;s blog<br \/>\nposts on <a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/05\/10\/memory-management-for-functions-and-variables\/\">Memory Management for Functions and Variables<\/a> to learn more about &#8220;lazy copy&#8221;.<\/p>\n<p><b>Comments<\/b><\/p>\n<p>Over the years, I have created many few-line functions, just to make my life easier. What are some of your small, but useful<br \/>\nfunctions? Tell us <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2205#respond\">here<\/a>.<\/p>\n<p><script>\/\/ <![CDATA[\nfunction grabCode_1c8cc2339d86422e84578e49d73615fb() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='1c8cc2339d86422e84578e49d73615fb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1c8cc2339d86422e84578e49d73615fb';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Jiro Doke';\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n\n\n\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\n\n\n\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }\n\/\/ ]]><\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\"><a><span style=\"font-size: x-small; font-style: italic;\">Get<br \/>\nthe MATLAB code<br \/>\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>Published with MATLAB\u00ae 7.6<\/p>\n<p><!--\n1c8cc2339d86422e84578e49d73615fb ##### SOURCE BEGIN #####\n%%\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectId=1094142&objectType=author % Jiro>'s pick this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=8677&objectType=file % RENVAR> by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectId=27836&objectType=author % Duane Hanselman>.\n\n%%\n% Imagine that you defined a variable:\n\noldVar = magic(3);\n\n%%\n% Now you want to rename this variable because the meaning of it has\n% changed. How do you do it? Do you do it interactively from the Workspace\n% Browser?\n%\n% <<renamevars_workspace.png>>\n\n%%\n% Or perhaps programmatically:\n\nnewVar = oldVar;\nclear oldVar\n\n%%\n% Duane's |renvar| let's you do this in a single swoop.\n\nrenvar newVar veryNewVar\n\n%%\n% If you look inside his function, it is basically executing the two lines\n% from above. I like this for a couple of reasons:\n%\n% * One line is shorter than two.\n% * It has a nice nugget of information regarding memory allocation.\n%\n% From the H1 line:\n%\n%    RENVAR Rename Variable Without Memory Reallocation.\n%\n% Yes, in fact this method (as well as the two-liner) does not reallocate\n% memory. No temporary memory is used, because it is doing \"lazy copying\".\n% So there's no need to worry even if you are renaming a very large\n% variable. Take a look at Loren's blog posts on\n% <https:\/\/blogs.mathworks.com\/loren\/2006\/05\/10\/memory-management-for-functions-and-variables\/ % Memory Management for Functions and Variables> to learn more about \"lazy\n% copy\".\n%\n% *Comments*\n%\n% Over the years, I have created many few-line functions, just to make my\n% life easier. What are some of your small, but useful functions? Tell us\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2205#respond here>.\n##### SOURCE END ##### 1c8cc2339d86422e84578e49d73615fb\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p> NoteThe file submission referenced in this post is no longer available on File Exchange.<br \/>\nJiro&#8217;s pick this week is RENVAR by Duane Hanselman.<br \/>\n&nbsp;<br \/>\nImagine that you defined a variable:<br \/>\noldVar&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/04\/18\/renaming-variables\/\">read more >><\/a><\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2205"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2205"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2205\/revisions"}],"predecessor-version":[{"id":8054,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2205\/revisions\/8054"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}