{"id":5452,"date":"2014-07-25T09:00:56","date_gmt":"2014-07-25T13:00:56","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5452"},"modified":"2014-07-24T01:36:38","modified_gmt":"2014-07-24T05:36:38","slug":"append-entries-to-a-legend","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/07\/25\/append-entries-to-a-legend\/","title":{"rendered":"Append entries to a legend"},"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\/47228-legappend\"><tt>legappend<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/225623\">Chad Greene<\/a>.\r\n   <\/p>\r\n   <p>Chad is no stranger to MATLAB Central. He has <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=authorid%3A225623\">over 50 File Exchange entries<\/a>, and two of his entries have been highlighted (<a href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/07\/27\/1000-unit-converters-at-your-fingertips\/\">unit converters<\/a> and <a href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/07\/19\/clear-everything\/\">ccc<\/a>) in Pick of the Week. His entries are well-written, and like this one, many of his entries have <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/publishing-matlab-code.html\">published example files<\/a>.\r\n   <\/p>\r\n   <p>Many of you may know that the command <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/legend.html\"><tt>legend<\/tt><\/a> creates one legend per axes.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">t = 0:.1:10;\r\nx1 = sin(t);\r\nx2 = cos(t);\r\nplot(t,x1,t,x2)\r\nlegend(<span style=\"color: #A020F0\">'Sine'<\/span>,<span style=\"color: #A020F0\">'Cosine'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_legendappend\/potw_legendappend_01.png\"> <p>Let's say that you wanted to add another line to this plot.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x3 = sin(t).*cos(t);\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(t,x3,<span style=\"color: #A020F0\">'--r'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_legendappend\/potw_legendappend_02.png\"> <p>To add this entry to the legend, you would re-run the <tt>legend<\/tt> command with the three entries.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">legend(<span style=\"color: #A020F0\">'Sine'<\/span>,<span style=\"color: #A020F0\">'Cosine'<\/span>,<span style=\"color: #A020F0\">'Sine*Cosine'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_legendappend\/potw_legendappend_03.png\"> <p>Chad's <tt>legappend<\/tt> allows you to append new entries to an existing legend. This means that you can simply call it along with the new lines you\r\n      create.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% New figure<\/span>\r\nfigure;\r\nplot(t,x1,t,x2)\r\nlegend(<span style=\"color: #A020F0\">'Sine'<\/span>,<span style=\"color: #A020F0\">'Cosine'<\/span>)\r\n\r\n<span style=\"color: #228B22\">% Add another line<\/span>\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(t,x3,<span style=\"color: #A020F0\">'--r'<\/span>)\r\n\r\n<span style=\"color: #228B22\">% Append a new legend item<\/span>\r\nlegappend(<span style=\"color: #A020F0\">'Sine*Cosine'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_legendappend\/potw_legendappend_04.png\"> <p>Great! You can also delete the last legend entry with the following command.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">legappend(<span style=\"color: #A020F0\">''<\/span>)<\/pre><p>If you want to see more examples, check out his published <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47228-legappend\/content\/legappend\/html\/legappend_demo.html\">example<\/a>.\r\n   <\/p>\r\n   <p>Chad explains that <tt>legappend<\/tt> simply deletes the current legend and recreates the updated legend. This is a good example of how you can take a normal mode\r\n      of operation and tweak it in order to adapt it to your needs.\r\n   <\/p>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5452#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47228-legappend#comments\">comment<\/a> for Chad.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_8e89d1b871044d998dd7701e2979a61f() {\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='8e89d1b871044d998dd7701e2979a61f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 8e89d1b871044d998dd7701e2979a61f';\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 = '';\r\n        copyright = 'Copyright 2014 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_8e89d1b871044d998dd7701e2979a61f()\"><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; R2014a<br><\/p>\r\n<\/div>\r\n<!--\r\n8e89d1b871044d998dd7701e2979a61f ##### 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\/47228-legappend |legappend|>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/225623\r\n% Chad Greene>.\r\n%\r\n% Chad is no stranger to MATLAB Central. He has\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=authorid%3A225623\r\n% over 50 File Exchange entries>, and two of his entries have been\r\n% highlighted\r\n% (<https:\/\/blogs.mathworks.com\/pick\/2012\/07\/27\/1000-unit-converters-at-your-fingertips\/\r\n% unit converters> and\r\n% <https:\/\/blogs.mathworks.com\/pick\/2013\/07\/19\/clear-everything\/ ccc>) in\r\n% Pick of the Week. His entries are well-written, and like this one, many\r\n% of his entries have\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/publishing-matlab-code.html\r\n% published example files>.\r\n%\r\n% Many of you may know that the command\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/legend.html |legend|> creates\r\n% one legend per axes.\r\n\r\nt = 0:.1:10;\r\nx1 = sin(t);\r\nx2 = cos(t);\r\nplot(t,x1,t,x2)\r\nlegend('Sine','Cosine')\r\n\r\n%%\r\n% Let's say that you wanted to add another line to this plot.\r\n\r\nx3 = sin(t).*cos(t);\r\nhold on\r\nplot(t,x3,'REPLACE_WITH_DASH_DASHr')\r\n\r\n%%\r\n% To add this entry to the legend, you would re-run the |legend| command\r\n% with the three entries.\r\n\r\nlegend('Sine','Cosine','Sine*Cosine')\r\n\r\n%%\r\n% Chad's |legappend| allows you to append new entries to an existing\r\n% legend. This means that you can simply call it along with the new lines\r\n% you create.\r\n\r\n% New figure\r\nfigure;\r\nplot(t,x1,t,x2)\r\nlegend('Sine','Cosine')\r\n\r\n% Add another line\r\nhold on\r\nplot(t,x3,'REPLACE_WITH_DASH_DASHr')\r\n\r\n% Append a new legend item\r\nlegappend('Sine*Cosine')\r\n\r\n%%\r\n% Great! You can also delete the last legend entry with the following\r\n% command.\r\n%\r\n%   legappend('')\r\n%\r\n% If you want to see more examples, check out his published\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47228-legappend\/content\/legappend\/html\/legappend_demo.html\r\n% example>.\r\n%\r\n% Chad explains that |legappend| simply deletes the current legend and\r\n% recreates the updated legend. This is a good example of how you can take\r\n% a normal mode of operation and tweak it in order to adapt it to your\r\n% needs.\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5452#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47228-legappend#comments\r\n% comment> for Chad.\r\n\r\n##### SOURCE END ##### 8e89d1b871044d998dd7701e2979a61f\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_legendappend\/potw_legendappend_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   Jiro's pick this week is legappend by Chad Greene.\r\n   \r\n   Chad is no stranger to MATLAB Central. He has over 50 File Exchange entries, and two of his entries have been highlighted (unit... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/07\/25\/append-entries-to-a-legend\/\">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\/5452"}],"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=5452"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5452\/revisions"}],"predecessor-version":[{"id":5456,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5452\/revisions\/5456"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}