File Exchange

image thumbnail

resizeLegend(vararg​in)

version 1.2.0.0 (11.2 KB) by David J. Mack
Shortens a legend

24 Downloads

Updated 29 Jun 2018

View License

This functions goes to extreme lengths to create a shortened legend, e.g. a legend with shorter lines segments then the standard legend. It is intended to be used before saving a figure. Any changes to the axes after the resized legend is created may result in strange behavior.
Example:
figure;
plot(rand(10,3));
resizeLegend();

Comments and Ratings (13)

Sara Hesami

Thank you. When I run the function the legend box become transparent! Any suggestions?

Dear all, I left science and thus my Matlab License... if anybody wants to take over - feel free to do it. I cannot provide any updates/bug fixes anymore.
Best regards, David

There seems to be a problem when both point and line type styles are used in the plot.
Your function produces the error:

The logical indices contain a true value outside of the array bounds
Error in resizeLegend (line 207)
HMarkers(i).XData = mean(HLines(IsMarker).XData);

Code that causes the error:
plot(x,y0,'o',x,y1,'--')
[hLegend,icons,plots,txt] = legend('test 1','test 2')
resizeLegend()

Hey Irene! Thanks for the tip! Could you provide a minimal example to reproduce the error? And the Matlab-Version you are using?

Irene Hu

I got that error, too, but it went away when I made my original legend using [hLegend,icons,plots,txt] = legend(labels,'Location','NorthEast'); instead of just hLegend = legend(labels,'Location','NorthEast');

@Nico MH: As always, it is tremendously helpful if you can provide a scenario to reproduce the error. Otherwise its nearly impossible to find the cause.

Nico MH

No appropriate method, property, or field 'Extent' for class 'matlab.graphics.GraphicsPlaceholder'.

@Fillippo & ths1104 Could you provide a minimal "working" example to reproduce the bug? I suppose this is related to some property which I did not take into account ('matlab.graphics.GraphicsPlaceholder' points to an uninitialized graphics object).

@ smsinks: Just tried the example. Did work for me. The synopsis lists exactly what you have to call. bar(rand(10,3)); resizeLegend() works fine for me.

No appropriate method, property, or field 'Extent' for class 'matlab.graphics.GraphicsPlaceholder'.

Error in resizeLegend>getLegSize (line 290)
ExtentsInPts = vertcat(HLeg.UserData.HTexts.Extent);

Error in resizeLegend (line 219)
Size = getLegSize(HLeg);

smsinks

Your code does not have an example. It is very difficult to understand what you say about the inputs, thus an example would help.
E.g. Resize legend on a bar graph with the following code:
bar(x)
resizeLegned( Inputs here)

Your example does not work. Unfortunately. Therefore, I will not rate your code.

No appropriate method, property, or field 'Extent' for class
'matlab.graphics.GraphicsPlaceholder'.

Error in resizeLegend>getLegSize (line 290)
ExtentsInPts = vertcat(HLeg.UserData.HTexts.Extent);

Error in resizeLegend (line 219)
Size = getLegSize(HLeg);

An example please

Updates

1.2.0.0

Fixed default behavior and added an example

1.1.0.0

[Fix] Now correclty treats legend entries of groups.
[Fix] Now correclty identifies the legend with mulitple plots in a figure.

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