View License

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

» Watch video

Highlights from

5.0 | 2 ratings Rate this file 10 Downloads (last 30 days) File Size: 3.48 KB File ID: #24510 Version: 1.0




22 Jun 2009 (Updated )

Shrinks the length of the legend lines.

| Watch this File

File Information

**Matlab no longer allows access to the elements within a legend, so this code will not work from Matlab circa-2014 onwards.**
legendshrink reduces the amount of horizontal space taken up by a figure legend by reducing the length of lines. (Best results when the legend box is off.)

This is important for small plots because the size of the lines is
constant irrespective of the physical size of the figure. Only works
for vertical legends, for now.

MATLAB release MATLAB 8.2 (R2013b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
03 Feb 2016 Matthew


I wrote a similar function that also broke with the new Matlab release. In order to fix your function, you just need to remove the call to the legend handle to retrieve the 'Children':

lch = get(lg,'Children'); %(get rid of this)

With the new Matlab behavior, you no longer need the 'Children', you can just go straight into the legend object handle to get the legend objects. In other words, instead of:

cch = findobj(lch,'-not','Tag','legendlabel');

you can use:

[l,lobj] = legend; (this would be done by the user and then lobj would be passed to the function)
cch = findobj(lobj,'-not','Tag','legendlabel');

I have not tried to fix your function based on the similarity with my function (not published) I think my fix will work or at least help you out.

Comment only
04 Sep 2015 Will Robertson

Thanks for letting me know. Matlab no longer appears to allow getting access to the lines inside of a legend -- e.g., writing the following results in an empty array:

h = legend;
hch = get(h,'children')

Given this, I don't think it's possible to "shrink" a Matlab legend any more :( You'd have to write entirely new code to draw a legend manually.

Comment only
03 Sep 2015 Daisuke Takeshita

It seems the code doesn't work for 2014b. Is there any plan to modify the code?

Comment only
04 May 2014 Adam Danz

Adam Danz (view profile)

Thanks for the code. I have a vertical legend in the upper right corner with two elements within the legend. The code is shrinking one of the elements as described but upon the second loop, I get the following error:

Error using hg.hggroup/get
The name 'XData' is not an accessible property for an instance of class 'hggroup'.
Error in legendshrink (line 114)
linepos = get(cch(ii),'XData');

Comment only
03 Apr 2014 Morteza

Excellent. Thanks for the script.

31 Oct 2013 Philip Ohnewein

Works nicely (when used with legend box off), but unfortunately it does not work when printing a figure. With print preview, the legend is set back to the way it looks without legendshrink.

Comment only
25 Apr 2013 Leo Simon

I'd prefer to leave the legend box on. Is there any way of shrinking the box as you shrink the lines?

Comment only
16 Dec 2009 Ross

Ross (view profile)

Excellent little script. Did exactly what I needed it to do for a small figure.

04 Sep 2015 1.0

Matlab no longer allows access to the elements within a legend, so this code will not work from Matlab circa-2014 onwards.

Contact us