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

10 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
figure;
plot(x,y, 'DisplayName', '$$y(\theta)=(a + b) sin(\theta) - b\,sin({a + b\over b }\theta)$$');
lgnd = legend('show');
set(lgnd,'Interpreter','latex')
%%create figure 2
figure;
plot(x,y, 'DisplayName', '$$y(\theta)=(a + b) sin(\theta) - b\,sin({a + b\over b }\theta)$$');
[lgnd, icons, ~, ~] = legend('show');
set(lgnd,'Interpreter','latex')
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?
Tobi
  2 Comments
Ced
Ced on 8 Mar 2016
Edited: Ced on 8 Mar 2016
Good question, never noticed this.
In the legend help, they mention this:
[l,icons,plots,txt] = legend [...]
Note: This syntax is not recommended and creates a legend that does not support all graphics features. Use the l = legend() syntax to return the legend object and set Legend Properties instead.
The legend object returned in both cases looks identical, but was generated slightly differently. What happens inside legend when you request the actual objects is that the "doPostSetup" method is called with "on" vs "off" in the case of a single output. Unfortunately, I don't have access to that method, so I'm not quite sure what's going on internally.
You can partially reverse this with:
figure;
plot(x,y, 'DisplayName', '$$y(\theta)=(a + b) sin(\theta) - b\,sin({a + b\over b }\theta)$$');
[lgnd2, ~, ~, ~] = legend('show');
lgnd2.doPostSetup('off')
set(lgnd2,'Interpreter','latex')
Then the interpreter seems to work again, but the rest is a messed up, so not really a solution either.
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
Ced on 9 Mar 2016
Edited: Ced on 10 Mar 2016
Running
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.
  2 Comments
Tobi.
Tobi. on 10 Mar 2016
That works, thanks for the idea. I still don't think of this as a perfect solution, as changing global parameters is something I shy away from, but it's waaaaay better than my workaround.
Thanks a lot!
Tobi
Walter Roberson
Walter Roberson on 10 Mar 2016
Edited: Walter Roberson on 10 Mar 2016
Instead of just
figure()
try
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);
figure;
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.
Tobi. on 8 Mar 2016
Edited: Tobi. on 8 Mar 2016
Mike,
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 '$$']);
end
end
end
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.
Tobi

Products

Community Treasure Hunt

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

Start Hunting!