What's the difference between hleg = legend(...) and [hleg, a, b, c] = legend(...)?
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Ced
2016-3-8
编辑:Ced
2016-3-8
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
2016-3-8
What release and operating system are you using? Are you using hardware OpenGL, software OpenGL, or painters as your renderer?
采纳的回答
Ced
2016-3-9
编辑:Ced
2016-3-10
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 个评论
Walter Roberson
2016-3-10
编辑:Walter Roberson
2016-3-10
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.)
更多回答(3 个)
Alexander Mueller
2017-3-15
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 个评论
Mike Garrity
2016-3-8
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!