What's the difference between hleg = legend(...) and [hleg, a, b, c] = legend(...)?
7 views (last 30 days)
Show older comments
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
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
on 8 Mar 2016
What release and operating system are you using? Are you using hardware OpenGL, software OpenGL, or painters as your renderer?
Accepted Answer
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
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.)
More Answers (3)
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.
0 Comments
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?
0 Comments
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!