{"id":4807,"date":"2013-09-13T09:00:22","date_gmt":"2013-09-13T13:00:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4807"},"modified":"2013-09-13T09:11:50","modified_gmt":"2013-09-13T13:11:50","slug":"4807","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/09\/13\/4807\/","title":{"rendered":"Easy labeling of a figure or axes"},"content":{"rendered":"<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\/41475-bordertext--varargin--\">\"Bordertext\"<\/a>, by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/340291\">Jan de Wilde<\/a>.<\/p><p>With \"bordertext,\" Jan has shared an elegant way to label a figure or an axes, saving us the trouble of having to calculate positions for our text. The function takes as an input a string indicating where you want your text. Thus, simply by specifying 'top,' 'bottom,' 'left,', 'right,' 'center', or 'userdefined,' you can position even multiline strings automatically, with respect to the current axes.<\/p><p>Moreover, you can modify those input strings with the prefix 'inner' (in the cases where it makes sense to do so) to make them refer to the interior of your axes, or with 'figure' to make them refer to figure positions. For instance:<\/p><pre class=\"language-matlab\">modOpts = {<span class=\"string\">''<\/span>,<span class=\"string\">'inner'<\/span>,<span class=\"string\">'figure'<\/span>};\r\nmainOpts = {<span class=\"string\">'topleft'<\/span>,<span class=\"string\">'top'<\/span>,<span class=\"string\">'topright'<\/span>,<span class=\"string\">'right'<\/span>,<span class=\"keyword\">...<\/span>\r\n   <span class=\"string\">'rightbottom'<\/span>,<span class=\"string\">'bottom'<\/span>,<span class=\"string\">'bottomleft'<\/span>,<span class=\"string\">'left'<\/span>,<span class=\"string\">'lefttop'<\/span>,<span class=\"string\">'center'<\/span>};\r\ncolors = jet(32);\r\n<span class=\"keyword\">for<\/span> ii = 1:15\r\n    color = colors(randi(size(colors,1)),:);\r\n    modOpt = modOpts{randi(numel(modOpts),1)};\r\n    mainOpt = mainOpts{randi(numel(mainOpts),1)};\r\n    <span class=\"keyword\">if<\/span> strcmp(mainOpt,<span class=\"string\">'center'<\/span>),modOpt = <span class=\"string\">''<\/span>;<span class=\"keyword\">end<\/span>\r\n    option = [modOpt,mainOpt];\r\n    bordertext(option,option,<span class=\"keyword\">...<\/span>\r\n        <span class=\"string\">'Color'<\/span>,color,<span class=\"string\">'BackgroundColor'<\/span>,imcomplement(color), <span class=\"keyword\">...<\/span>\r\n        <span class=\"string\">'FontSize'<\/span>,18);\r\n    pause(1.5)\r\n    bordertext(option,<span class=\"string\">''<\/span>);\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/capture-11.gif\" alt=\"\"> <\/p><p>Additionally, one can specify a host of formatting commands to control the style of the text.<\/p><p>Jan's code is very nicely written and documented. Plus, I like that he was responsive to a comment requesting support for \"standard [MATLAB] syntax.\" (I garner that the file originally required format strings to be specified in a non-standard fashion. That format option is still available, and convenient, but I agree with the commenter that the standard format is easier to use.)<\/p><p>As always, I welcome your <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4807#respond\">thoughts and comments<\/a>. Or leave feedback for Jan <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/41475-bordertext--varargin--#comments\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_4e4d1f6c006b4ddf82795d1b7e54d465() {\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='4e4d1f6c006b4ddf82795d1b7e54d465 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4e4d1f6c006b4ddf82795d1b7e54d465';\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_4e4d1f6c006b4ddf82795d1b7e54d465()\"><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\n4e4d1f6c006b4ddf82795d1b7e54d465 ##### SOURCE BEGIN #####\r\n%% Easy labeling of a figure or axes\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/41475-bordertext--varargin-- \"Bordertext\">, by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/340291 Jan de Wilde>.\r\n\r\n%%\r\n% With \"bordertext,\" Jan has shared an elegant way to label a figure or an\r\n% axes, saving us the trouble of having to calculate positions for our\r\n% text. The function takes as an input a string indicating where you want\r\n% your text. Thus, simply by specifying 'top,' 'bottom,' 'left,', 'right,'\r\n% 'center', or 'userdefined,' you can position even multiline strings\r\n% automatically, with respect to the current axes.\r\n\r\n%% \r\n% Moreover, you can modify those input strings with the prefix 'inner' (in\r\n% the cases where it makes sense to do so) to make them refer to the\r\n% interior of your axes, or with 'figure' to make them refer to figure\r\n% positions. For instance:\r\n\r\n%%\r\n% \r\n%   modOpts = {'','inner','figure'};\r\n%   mainOpts = {'topleft','top','topright','right',...\r\n%      'rightbottom','bottom','bottomleft','left','lefttop','center'};\r\n%   colors = jet(32);\r\n%   for ii = 1:15\r\n%       color = colors(randi(size(colors,1)),:);\r\n%       modOpt = modOpts{randi(numel(modOpts),1)};\r\n%       mainOpt = mainOpts{randi(numel(mainOpts),1)};\r\n%       if strcmp(mainOpt,'center'),modOpt = '';end\r\n%       option = [modOpt,mainOpt];\r\n%       bordertext(option,option,...\r\n%           'Color',color,'BackgroundColor',imcomplement(color), ...\r\n%           'FontSize',18);\r\n%       pause(1.5)\r\n%       bordertext(option,'');\r\n%   end\r\n\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/capture-11.gif>>\r\n% \r\n\r\n%%\r\n% Additionally, one can specify a host of formatting commands to control\r\n% the style of the text.\r\n\r\n%%\r\n% Jan's code is very nicely written and documented. Plus, I like that he was\r\n% responsive to a comment requesting support for \"standard [MATLAB]\r\n% syntax.\" (I garner that the file originally required format strings to be\r\n% specified in a non-standard fashion. That format option is still\r\n% available, and convenient, but I agree with the commenter that the\r\n% standard format is easier to use.)\r\n\r\n%%\r\n% As always, I welcome your\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=4807#respond thoughts and comments>.\r\n% Or leave feedback for Jan\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/41475-bordertext--varargin--#comments here>.\r\n##### SOURCE END ##### 4e4d1f6c006b4ddf82795d1b7e54d465\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/capture-11.gif\" onError=\"this.style.display ='none';\" \/><\/div><p>Brett's Pick this week is \"Bordertext\", by Jan de Wilde.With \"bordertext,\" Jan has shared an elegant way to label a figure or an axes, saving us the trouble of having to calculate positions for our... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/09\/13\/4807\/\">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\/4807"}],"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=4807"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4807\/revisions"}],"predecessor-version":[{"id":4818,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4807\/revisions\/4818"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}