File Exchange

image thumbnail

legappend

version 1.0 (45.2 KB) by

Append new entries to an existing legend

3.33333
3 Ratings

26 Downloads

Updated

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

The legappend function adds new entries to a legend. This is good for making PowerPoint slides, when you may want to start by plotting a few data sets, then add data to your plot one entry at a time. This function eliminates the need to copy and paste the complete legend creation code each time you add a curve to a plot, and alleviates the minor headaches you'd experience if you decided to rearrange the order in which items are plotted.

Description
legappend('new legend entry') appends an existing legend with "new legend entry".

legappend('new entry 1','new entry 2',...,'new entry N') adds several new entries to the legend.

legappend('') deletes the last entry from the legend.

legappend('','',...,'') deletes the last several entries from the legend.

[legend_h,object_h,plot_h,text_strings] = legappend(...) returns legend_h, the handle of the new legend; object_h, handles of the line, patch, and text graphics objects used in the legend; plot_h, handles of the lines and other objects used in the plot; and text_strings, a cell array of the text strings used in the legend. Note that for new legend entries, legappend does not add entries to a current legend, but deletes the current legend and recreates a new one. As a result, the legend handle will change with each new-entry use of legappend. The legend handle does not change when legappend is used to delete an entry.

Comments and Ratings (5)

Sam

Sam (view profile)

For those with issues in MATLAB versions R2014b and newer, this code is written assuming the legend is an axes object embedded in your current axes. This behavior was changed in R2014b, which introduced legends as their own object.

A simple baseline fix can be made with the following for a single new legend entry:

h = findobj(gcf, 'Type', 'legend');
newstr = [h.String {newStrings}];
allDatah = flipud(get(gca,'children'));
h.PlotChildren = allDatah;
h.String = newstr;

Adam Goodkind

I receive the following error when trying to use legappend() in a loop:

Error using legend>process_inputs (line 526)
Invalid argument. Type 'help legend' for more information.

Error in legend>make_legend (line 303)
[orient,location,position,children,listen,strings,propargs] =
process_inputs(ha,argin); %#ok

Error in legend (line 257)
[~,msg] = make_legend(ha,args(arg:end),version);

Error in legappend (line 74)
[legend_h,object_h,plot_h,text_strings] = legend(h,allDatah,str);

Dark Dragon

Would be nice to support handle parameter to add specific line instead of last one to legend.

Anders

Anders (view profile)

Hi Chad

I am using the MATLAB with the new graphics engine since 2014 and it causes an error in your demo script. They have changed the behavior of the legend so much so that it is incompatible with alot of my existing code. :-(

Chen Qi

Chen Qi (view profile)

MATLAB Release
MATLAB 8.0 (R2012b)
Acknowledgements

Inspired by: grayout

Inspired: legalpha, label, HG2 Legend Tools

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today

legappend/html/