{"id":2555,"date":"2010-05-07T13:30:59","date_gmt":"2010-05-07T13:30:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2010\/05\/07\/putting-variables-back-in-the-base-workspace\/"},"modified":"2010-05-07T13:30:59","modified_gmt":"2010-05-07T13:30:59","slug":"putting-variables-back-in-the-base-workspace","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2010\/05\/07\/putting-variables-back-in-the-base-workspace\/","title":{"rendered":"Putting Variables Back in the Base Workspace"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27106-putvar--uigetvar-putvar\">PUTVAR<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/679\">John D'Errico<\/a>.\r\n   <\/p>\r\n   <p>This is an interesting one. The more I think about it, the more I find it useful. <tt>putvar<\/tt> is a simple function for saving local variables to the base workspace, without requiring any output arguments to your function.\r\n      It works similarly to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/assignin.html\"><tt>assignin<\/tt><\/a>, but it can do multiple variables at once.\r\n   <\/p>\r\n   <p>In general, my stance is that I prefer not using commands like <tt>assignin<\/tt>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/eval.html\"><tt>eval<\/tt><\/a>, or <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/evalin.html\"><tt>evalin<\/tt><\/a> to create variables because there are usually other more programmatically sound ways to achieve the same end goal (<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_prog\/br04bw6-98.html\">cell arrays<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_prog\/br04bw6-38.html\">structures<\/a>, output arguments to a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_prog\/bresuxt-1.html\">function<\/a>, etc). However, the particular use case John mentions in the HELP resonates with me. When I'm in the debugger, I sometimes\r\n      want to save out some of the variables in the function back into the base workspace so that I have them available after I\r\n      exit debug mode. Instead of making a bunch of <tt>assignin<\/tt> calls for all the variables of interest:\r\n   <\/p><pre>  assignin('base', 'var1', var1)\r\n  assignin('base', 'var2', var2)\r\n  assignin('base', 'var3', var3)\r\n  assignin('base', 'var4', var4)<\/pre><p>I can simply make a single call to <tt>putvar<\/tt>:\r\n   <\/p><pre>  putvar(var1, var2, var3, var4)<\/pre><p>As usual (and expected), John's code is very well written -- nice help text with examples, extensive comments, error checking,\r\n      and warnings.\r\n   <\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Give this a try and <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2555#respond\">tell us<\/a> what you think, or leave a comment for John on the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27106-putvar--uigetvar-putvar\">submission page<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_b6caad0993ff45b6960e4c9abd54b2e5() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='b6caad0993ff45b6960e4c9abd54b2e5 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b6caad0993ff45b6960e4c9abd54b2e5';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Jiro Doke';\r\n        copyright = 'Copyright 2010 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<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');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_b6caad0993ff45b6960e4c9abd54b2e5()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.10<br><\/p>\r\n<\/div>\r\n<!--\r\nb6caad0993ff45b6960e4c9abd54b2e5 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27106-putvar--uigetvar-putvar\r\n% PUTVAR> by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/679\r\n% John D'Errico>.\r\n%\r\n% This is an interesting one. The more I think about it, the more I find it\r\n% useful. |putvar| is a simple function for saving local variables to the\r\n% base workspace, without requiring any output arguments to your function.\r\n% It works similarly to\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/assignin.html\r\n% |assignin|>, but it can do multiple variables at once.\r\n% \r\n% In general, my stance is that I prefer not using commands like\r\n% |assignin|,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/eval.html\r\n% |eval|>, or\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/evalin.html\r\n% |evalin|> to create variables because there are usually other more\r\n% programmatically sound ways to achieve the same end goal\r\n% (<https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_prog\/br04bw6-98.html\r\n% cell arrays>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_prog\/br04bw6-38.html\r\n% structures>, output arguments to a\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_prog\/bresuxt-1.html\r\n% function>, etc). However, the particular use case John mentions in\r\n% the HELP resonates with me. When I'm in the debugger, I sometimes want to\r\n% save out some of the variables in the function back into the base\r\n% workspace so that I have them available after I exit debug mode. Instead\r\n% of making a bunch of |assignin| calls for all the variables of interest:\r\n%\r\n%    assignin('base', 'var1', var1)\r\n%    assignin('base', 'var2', var2)\r\n%    assignin('base', 'var3', var3)\r\n%    assignin('base', 'var4', var4)\r\n%\r\n% I can simply make a single call to |putvar|:\r\n%\r\n%    putvar(var1, var2, var3, var4)\r\n%\r\n% As usual (and expected), John's code is very well written REPLACE_WITH_DASH_DASH nice help\r\n% text with examples, extensive comments, error checking, and warnings.\r\n%\r\n% *Comments*\r\n%\r\n% Give this a try and <https:\/\/blogs.mathworks.com\/pick\/?p=2555#respond tell\r\n% us> what you think, or leave a comment for John on the  \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27106-putvar--uigetvar-putvar\r\n% submission page>.\r\n##### SOURCE END ##### b6caad0993ff45b6960e4c9abd54b2e5\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is PUTVAR by John D'Errico.\r\n   \r\n   This is an interesting one. The more I think about it, the more I find it useful. putvar is a simple function for saving local... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/05\/07\/putting-variables-back-in-the-base-workspace\/\">read more >><\/a><\/p>","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\/2555"}],"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=2555"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2555\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}