File Exchange

image thumbnail

legappend

version 1.0.0.0 (45.2 KB) by Chad Greene
Append new entries to an existing legend

41 Downloads

Updated 14 Jul 2014

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.

Cite As

Chad Greene (2020). legappend (https://www.mathworks.com/matlabcentral/fileexchange/47228-legappend), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)

Salman Khan

I wish someone could provide a way to use it with R2019b.

When I ran the following example, I got an error ?
t = 0:.1:10;
x1 = sin(t);
x2 = cos(t);
% New figure
figure;
plot(t,x1,t,x2)
legend('Sine','Cosine')

% Add another line
x3 = sin(t).*cos(t);
hold on
plot(t,x3,'--r')

% Append a new legend item
legappend('Sine*Cosine')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Error using legend (line 261)
Invalid argument. Type 'help legend' for more information.

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

Meade

Chad,

Good to see your name again. Another super helpful function.
One suggestion if you're ever in the mood to update...

You function will bonk if you ever attempt to add more legend entries than there are plot children. Perhaps a catch in there that automatically 'pads' the figure will invisible dummy data to catch this error. It was my bright idea... turns out I just wasn't anywhere close to the first person to have it. Who knew!?
https://stackoverflow.com/a/33475174

Best,
Meade

Tal kohavy

sco1 gave the best solution! (look above)
Highly recommended to copy paste what he did!
Thank you so much, sco1 :)

The solution suggested by sco1 works fine except for a minor issue. if sco1's code is used after the plot command then h.String returns the previous legend entries plus the dummy string "data1" for the last plotted data. So you want to replace the dummy strings with the correct legend entries.

plot(data1);
legend('mydata1');
hold on;
plot(data2);
plot(data3);
h = findobj(gcf, 'Type', 'legend');
newstr = [h.String{1:end-2} {'mydata2','mydata3'}];
allDatah = flipud(get(gca,'children'));
h.PlotChildren = allDatah;
h.String = newstr;

sco1

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;

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

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

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: grayout

Inspired: HG2 Legend Tools, label, legalpha

legappend/html/