File Exchange

image thumbnail


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


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.

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 (2021). legappend (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

Samantha Kulpinski

Hi All - I uploaded a second version which should work for versions of MATLAB R2016b and later: please provide feedback there! Thank you to Chad Greene for the tutorial and original code.

Nicolas Christen

Niels Thøgersen

Doesn't work


Salman Khan

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

Chang hsiung

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

% Add another line
x3 = sin(t).*cos(t);
hold on

% Append a new legend item
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);

Chang hsiung



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!?


James Greenbaum

karim khalifa

Pavel Kovalyov

Tal kohavy

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

Bharat Mahajan

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.

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


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.


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

Inspired by: grayout

Inspired: legappend, HG2 Legend Tools, label, legalpha

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!