{"id":4507,"date":"2013-04-19T09:00:48","date_gmt":"2013-04-19T13:00:48","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4507"},"modified":"2013-04-19T09:00:48","modified_gmt":"2013-04-19T13:00:48","slug":"manage-the-automatic-naming-of-your-figure-windows","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/04\/19\/manage-the-automatic-naming-of-your-figure-windows\/","title":{"rendered":"Manage the (Automatic) Naming of Your Figure Windows"},"content":{"rendered":"\n<div class=\"content\"><!--introduction--><!--\/introduction--><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33076-setname\">\"setname\"<\/a>, by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/20418\">Andrew Bliss<\/a>.<\/p><p>Some of you may be familiar with my own <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18220-create-and-activate-figures-by-name\"><tt>togglefig<\/tt><\/a> function; it allows one to recall and reuse a figure by specifying its name. I find it extremely practical, and I use it almost on a daily basis. (It's particularly useful when you're iterating on a block of code separated into <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/run-sections-of-programs.html#brqxeeu-271 (link no longer works)\">\"sections.\"<\/a> Andrew's <tt>setname<\/tt> makes a very nice companion to <tt>togglefig<\/tt>.)<\/p><p>As of this writing, Andrew has 10 files on the Exchange, which have collectively been downloaded more than 500 times in the past 30 days. (Not too shabby!) Nonetheless, I don't believe any of his submissions have previously been recognized as a Pick of the Week. Until now.<\/p><p>Ironically, the file of Andrew's that I am most enamored of is his least-downloaded.<\/p><p>So what does Andrew's function do? If you've previously indicated a title for the axes in your figure, <tt>setname<\/tt> automatically renames the figure to match axes name. If you haven't titled your graphic, the function extracts a string from the x- and y- labels of the axes to construct a figure name.<\/p><p>Consider, for example, this snippet of code:<\/p><pre class=\"language-matlab\">t = 0:pi\/64:8*pi;\nplot(t,sin(t)+rand(size(t)))\nxlabel(<span class=\"string\">'Time'<\/span>)\nylabel(<span class=\"string\">'Noisy Sine'<\/span>)\ntitle(<span class=\"string\">'Noisy Sinusoid'<\/span>)\n<\/pre><p>This creates a figure not-so-usefully named \"Untitled\" (with a number appended to differentiate it from the other <i>n<\/i> \"Untitled\" figures you've previously created.)<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/setname1.png\" alt=\"\"> <\/p><p>Now issue the <tt>setname<\/tt> command (no arguments needed--it works by default on the current axes, though you may specify a non-default axes.) Now the figure is renamed to \"Noisy Sinusoid,\" and you can readily pick it out from all the other open figures.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/setname2.png\" alt=\"\"> <\/p><p>Alternatively, if you didn't explicitly provide a title, <tt>setname<\/tt> would automatically name the figure \"Time vs Noisy Sine.\" Now not only can I find the figure, I can easily reuse it:<\/p><pre class=\"language-matlab\"><span class=\"comment\">% Recall the previous figure...<\/span>\ntogglefig(<span class=\"string\">'Noisy Sinusoid'<\/span>)\nplot(t,cos(t)+rand(size(t)))\ntitle(<span class=\"string\">'Noisy Cosine'<\/span>)\n<span class=\"comment\">% ...and then rename the figure!<\/span>\nsetname\n<\/pre><p>This is brilliant, Andrew...thanks! One suggestion: it would be really nice if I could pass to <tt>setname<\/tt> an argument ('all'?) that tells the function to rename all open figures. (For those times when I've lazily created 20 different 'Untitled' figures.)<\/p><p>As always, I welcome your <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4507#respond\">thoughts and comments<\/a>. Or leave feedback for Andrew <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33076-setname#comments\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \n    function grabCode_be14492a2d024f0388667063ce7da3f5() {\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='be14492a2d024f0388667063ce7da3f5 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' be14492a2d024f0388667063ce7da3f5';\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        copyright = 'Copyright 2013 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('<pre>\\n');\n        d.write(code_string);\n\n        \/\/ Add copyright line at the bottom if specified.\n        if (copyright.length > 0) {\n            d.writeln('');\n            d.writeln('%%');\n            if (copyright.length > 0) {\n                d.writeln('% _' + copyright + '_');\n            }\n        }\n\n        d.write('<\/pre>\\n');\n\n        d.title = title + ' (MATLAB code)';\n        d.close();\n    }   \n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_be14492a2d024f0388667063ce7da3f5()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\n      Published with MATLAB&reg; R2013a<br><\/p><p class=\"footer\"><br>\n      Published with MATLAB&reg; R2013a<br><\/p><\/div><!--\nbe14492a2d024f0388667063ce7da3f5 ##### SOURCE BEGIN #####\n%% Manage the (Automatic) Naming of Your Figure Windows\n%% \n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33076-setname \"setname\">, by\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/20418 Andrew Bliss>.\n\n%%\n% Some of you may be familiar with my own \n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18220-create-and-activate-figures-by-name |togglefig|> function; it allows\n% one to recall and reuse a figure by specifying its name. I find it\n% extremely practical, and I use it almost on a daily basis. (It's particularly useful \n% when you're iterating on a block of code separated into \n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/run-sections-of-programs.html#brqxeeu-271 \"sections.\"> \n% Andrew's |setname| makes a very nice companion to |togglefig|.)\n\n%%\n% As of this writing, Andrew has 10 files on the Exchange, which have\n% collectively been downloaded more than 500 times in the past 30 days.\n% (Not too shabby!) Nonetheless, I don't believe any of his submissions\n% have previously been recognized as a Pick of the Week. Until now. \n\n%%\n% Ironically, the file of Andrew's that I am most enamored of is his\n% least-downloaded.\n\n%%\n% So what does Andrew's function do? If you've previously indicated a title for the axes in your \n% figure, |setname| automatically renames the figure to match axes name. If you haven't \n% titled your graphic, the function extracts a string from the x- and y-\n% labels of the axes to construct a figure name.\n\n%%\n% Consider, for example, this snippet of code:\n%\n%   t = 0:pi\/64:8*pi;\n%   plot(t,sin(t)+rand(size(t)))\n%   xlabel('Time')\n%   ylabel('Noisy Sine')\n%   title('Noisy Sinusoid')\n\n%%\n% This creates a figure not-so-usefully named \"Untitled\" (with a number\n% appended to differentiate it from the other _n_ \"Untitled\" figures you've\n% previously created.)\n\n%%\n% \n% <<https:\/\/blogs.mathworks.com\/pick\/files\/setname1.png>>\n% \n\n%% \n% Now issue the |setname| command (no arguments neededREPLACE_WITH_DASH_DASHit works by default\n% on the current axes, though you may specify a non-default axes.) Now the\n% figure is renamed to \"Noisy Sinusoid,\" and you can readily pick it out\n% from all the other open figures.\n\n%%\n% \n% <<https:\/\/blogs.mathworks.com\/pick\/files\/setname2.png>>\n% \n\n%%\n% Alternatively, if you didn't explicitly provide a title, |setname| would\n% automatically name the figure \"Time vs Noisy Sine.\" Now not\n% only can I find the figure, I can easily reuse it:\n\n%%\n%   % Recall the previous figure...\n%   togglefig('Noisy Sinusoid')\n%   plot(t,cos(t)+rand(size(t)))\n%   title('Noisy Cosine')\n%   % ...and then rename the figure!\n%   setname\n\n%%\n% This is brilliant, Andrew...thanks! One suggestion: it would be really\n% nice if I could pass to |setname| an argument ('all'?) that tells the\n% function to rename all open figures. (For those times when I've lazily created 20\n% different 'Untitled' figures.)\n\n%% \n% As always, I welcome your\n% <https:\/\/blogs.mathworks.com\/pick\/?p=4507#respond thoughts and comments>.\n% Or leave feedback for Andrew <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33076-setname#comments here>.\n##### SOURCE END ##### be14492a2d024f0388667063ce7da3f5\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/setname1.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\nBrett's Pick this week is \"setname\", by Andrew Bliss.Some of you may be familiar with my own togglefig function; it allows one to recall and reuse a figure by specifying its name. I find it... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/04\/19\/manage-the-automatic-naming-of-your-figure-windows\/\">read more >><\/a><\/p>","protected":false},"author":34,"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\/4507"}],"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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=4507"}],"version-history":[{"count":15,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4507\/revisions"}],"predecessor-version":[{"id":4527,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4507\/revisions\/4527"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}