{"id":4528,"date":"2013-04-26T09:00:21","date_gmt":"2013-04-26T13:00:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4528"},"modified":"2013-04-25T14:43:40","modified_gmt":"2013-04-25T18:43:40","slug":"toggle-that-fig","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/04\/26\/toggle-that-fig\/","title":{"rendered":"Toggle that Fig"},"content":{"rendered":"\r\n<div class=\"content\"><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\/18220-create-and-activate-figures-by-name\"><tt>togglefig<\/tt><\/a> by our own <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett Shoelson<\/a>.<\/p><p>Brett and I, along with Bob, started writing for this blog about 5 years ago, back when <a href=\"https:\/\/blogs.mathworks.com\/videos\/\">Doug<\/a> was still the owner of the blog. Since the beginning, we typically highlighted submissions that were written by the general public and not by one of us. But ultimately, we like to highlight files that can be of some value to our readers, and I must say that there's no shortage of such files in <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=authorid%3A911\">Brett's entries<\/a>.<\/p><p>So what does <tt>togglefig<\/tt> do? Have you ever wanted to better manage your figure windows? I've often had a need to reuse my figure windows. Once, I was developing some piece of code that created multiple figures, and as I was iterating on my code, I kept creating the same number of windows each time, and I would end up with tens of figure windows open after a while. Of course I could \"close all\" of them at the beginning of my script, but I had some windows I needed open for other reasons. As a solution, I would create\/select figures using some numbers, e.g.<\/p><pre class=\"language-matlab\">figure(123)\r\n<\/pre><p>This would always use the same figure window, or create one if it doesn't exist. But I would want to give a meaningful name to it, so then I would need to set the name property after getting the handle to the figure.<\/p><pre class=\"codeinput\">h = figure(123);\r\nset(h, <span class=\"string\">'Name'<\/span>, <span class=\"string\">'Important Window'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_togglefig\/potw_togglefig_01.png\" alt=\"\"> <p>It works, but it's just some extra lines of code that are not adding any value to the real purpose of my program.<\/p><p>Brett's <tt>togglefig<\/tt> addresses this in a very elegant manner. It would raise the figure with the specified name, or create one if it doesn't exist.<\/p><pre class=\"codeinput\">togglefig(<span class=\"string\">'More Important Window'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_togglefig\/potw_togglefig_02.png\" alt=\"\"> <p>You can also pass in an optional argument to clear the figure window when it's given focus.<\/p><p>Thanks, Brett, for this extremely useful function that makes me more efficient in using MATLAB!<\/p><p><b>Comments<\/b><\/p><p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4528#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18220-create-and-activate-figures-by-name#comments\">comment<\/a> for Brett.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_f934efc8e76540b9b28977f011da5aba() {\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='f934efc8e76540b9b28977f011da5aba ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f934efc8e76540b9b28977f011da5aba';\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        copyright = 'Copyright 2013 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_f934efc8e76540b9b28977f011da5aba()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p><\/div><!--\r\nf934efc8e76540b9b28977f011da5aba ##### 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\/18220-create-and-activate-figures-by-name |togglefig|>\r\n% by our own\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett\r\n% Shoelson>.\r\n%\r\n% Brett and I, along with Bob, started writing for this blog about 5 years\r\n% ago, back when <https:\/\/blogs.mathworks.com\/videos\/ Doug> was still the\r\n% owner of the blog. Since the beginning, we typically highlighted\r\n% submissions that were written by the general public and not by one of us.\r\n% But ultimately, we like to highlight files that can be of some value to\r\n% our readers, and I must say that there's no shortage of such files in\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=authorid%3A911\r\n% Brett's entries>.\r\n%\r\n% So what does |togglefig| do? Have you ever wanted to better manage your\r\n% figure windows? I've often had a need to reuse my figure windows. Once, I\r\n% was developing some piece of code that created multiple figures, and as I\r\n% was iterating on my code, I kept creating the same number of windows each\r\n% time, and I would end up with tens of figure windows open after a while.\r\n% Of course I could \"close all\" of them at the beginning of my script, but\r\n% I had some windows I needed open for other reasons. As a solution, I\r\n% would create\/select figures using some numbers, e.g.\r\n%\r\n%   figure(123)\r\n%\r\n% This would always use the same figure window, or create one if it doesn't\r\n% exist. But I would want to give a meaningful name to it, so then I would\r\n% need to set the name property after getting the handle to the figure.\r\n\r\nh = figure(123);\r\nset(h, 'Name', 'Important Window');\r\n\r\n%%\r\n% It works, but it's just some extra lines of code that are not adding any\r\n% value to the real purpose of my program.\r\n%\r\n% Brett's |togglefig| addresses this in a very elegant manner. It would raise\r\n% the figure with the specified name, or create one if it doesn't exist.\r\n\r\ntogglefig('More Important Window')\r\n\r\n%%\r\n% You can also pass in an optional argument to clear the figure window when\r\n% it's given focus.\r\n% \r\n% Thanks, Brett, for this extremely useful function that makes me more\r\n% efficient in using MATLAB!\r\n% \r\n% *Comments*\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=4528#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18220-create-and-activate-figures-by-name#comments\r\n% comment> for Brett.\r\n\r\n##### SOURCE END ##### f934efc8e76540b9b28977f011da5aba\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_togglefig\/potw_togglefig_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nJiro's pick this week is togglefig by our own Brett Shoelson.Brett and I, along with Bob, started writing for this blog about 5 years ago, back when Doug was still the owner of the blog. Since the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/04\/26\/toggle-that-fig\/\">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\/4528"}],"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=4528"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4528\/revisions"}],"predecessor-version":[{"id":4533,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4528\/revisions\/4533"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}