What's the difference between hleg = legend(...) and [hleg, a, b, c] = legend(...)?

15 views (last 30 days)
Hi there,
legend() behaves differently when being used with a different number of output arguments:
theta = 0:0.01:2*pi;
a = 1;
b = 0.05;
x = (a + b)*cos(theta) - b*cos((a + b)/b*theta);
y = (a + b)*sin(theta) - b*sin((a + b)/b*theta);
%%create figure 1
plot(x,y, 'DisplayName', '$$y(\theta)=(a + b) sin(\theta) - b\,sin({a + b\over b }\theta)$$');
lgnd = legend('show');
%%create figure 2
plot(x,y, 'DisplayName', '$$y(\theta)=(a + b) sin(\theta) - b\,sin({a + b\over b }\theta)$$');
[lgnd, icons, ~, ~] = legend('show');
Note that the only difference is the return values of legend('show').
In my opinion, there's no difference with regard to lgnd, but setting the LaTeX interpreter in the second figure fails. How come?
Steven Lord
Steven Lord on 8 Mar 2016
What release and operating system are you using? Are you using hardware OpenGL, software OpenGL, or painters as your renderer?

Sign in to comment.

Accepted Answer

Ced on 9 Mar 2016
Edited: Ced on 10 Mar 2016
set(0, 'DefaultLegendInterpreter', 'latex')
before plotting fixes the legend textinterpreter for me. You could give that a try.
*EDIT*: As correctly pointed out by Walter, this setting is new in R2014b.
Walter Roberson
Walter Roberson on 10 Mar 2016
Edited: Walter Roberson on 10 Mar 2016
Instead of just
figure('DefaultFigureLegendInterpreter', 'latex')
This doesn't work for R2014a or before (but neither does Ced's suggestion, not for those releases.)

Sign in to comment.

More Answers (3)

Alexander Mueller
Alexander Mueller on 15 Mar 2017
As of R2017a you can specify the Interpreter by providing the name/value pair to the multiple output arg form of legend.
theta = 0:0.01:2*pi;
a = 1;
b = 0.05;
x = (a + b)*cos(theta) - b*cos((a + b)/b*theta);
y = (a + b)*sin(theta) - b*sin((a + b)/b*theta);
plot(x,y, 'DisplayName', '$$y(\theta)=(a + b) sin(\theta) - b\,sin({a + b\over b }\theta)$$');
[lgnd, icons, ~, ~] = legend('show',{},'Interpreter','latex');
You can set properties on the legend created by the multiple output argument form of the legend function, provided you specify them as name/value pairs to the legend function at the time of construction. Setting properties after construction is not guaranteed to work.

Mike Garrity
Mike Garrity on 8 Mar 2016
As Ced noted above, the four return arg form is deprecated and is only included for compatibility reasons. It is returning the old version of legend. If you've got old code which was using some of the more "edge case" features of the old version, then you might need to use this form. But if you do, then you're not going to be getting some of the newer features. The LaTeX interpreter may be one of the newer features. I'm not sure what release it was added in.
Is there a reason you need those other return arguments?

Tobi. on 8 Mar 2016
Edited: Tobi. on 8 Mar 2016
there is. I have results from multiple experiments and use plot() to indicate the estimated value \hat{E} over time, but include variance by creating a fill() with +-\hat{\sigma} around the estimated value. I'm then creating a legend for each fill area and draw a line across. This gives a good representation of \hat{E\+-\hat{\sigma}.
For this to work, I need to be able to find the coordinates of each patch in the legend, and this can be done by searching through the icons variable. I have not found a way to do this with just the legend, thus two return variables.
I have, by now, found a depressingly clumsy workaround for the original problem in question:
% crude workaround: set interpreter in legend and in each icon
set(lgnd, 'Interpreter', 'latex');
% cycle over all icons
for q = 1:length(icons)
% if icon is a patch, draw a line with basecolor across
if isprop(icons(q), 'FaceColor')
hl = line(icons(q).XData([1 3]), mean(icons(q).YData([1 2]))*[1 1], 'Color', floor(icons(q).FaceColor));
hl.Parent = icons(q).Parent;
% if icon is text field, change intepreter to latex and fix labeling
elseif isprop(icons(q), 'Interpreter')
hl = icons(q);
set(icons(q), 'Interpreter', 'latex');
entry = get(icons(q), 'String');
entry = strrep(entry, '\sigma', '\hat{\sigma}');
entry = strrep(entry, 'E(', '\hat{E}(');
% string was changed, update and enclose as equation
if ~strcmp(get(icons(q), 'String'), entry)
set(icons(q), 'String', ['$$' entry '$$']);
Yes, elegant it is not. But it works around the problem created with multiple outputs to legend(...) by changing each icon separately.
BTW: I'm using R2015b on Windows.


Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by