Subscripts in MATLAB Legends

This is a very basic question, and as my code suggests, it should work, but it's not: I want have the variables in these legend keys have subscripts, but when MATLAB displays the graph, they appear as written with the underscore symbol.
legend('e_1/e_{in}','e_2/e_{in}','e_{out}/e_{in}');
Thanks in advance for your help!

40 个评论

Quizmaster - the above code works when I try it. If you do
h = legend('e_1/e_{in}','e_2/e_{in}','e_{out}/e_{in}');
and then
get(h,'Interpreter')
what is returned? (The default should be tex.)
Still not working. Here is my complete coding:
clc
clear all
figure(1)
hold on
% e_1/e_in
N1 = (1);
D1 = [1.6e-5 1];
sys1 = tf(N1,D1);
% e_2/e_in
N2 = (2);
D2 = [1.6e-5 1];
sys2 = tf(N2,D2);
% e_out/e_in
N3 = (6.4e-4);
D3 = [5.12e-9 3.36e-4 1];
sys3 = tf(N3,D3);
bodemag(sys1,'-k',sys2,'--k',sys3,'-.k')
hold off
title('Bode Plot Evolution of Circuit Signal');
h = legend('e_1/e_{in}','e_2/e_{in}','e_{out}/e_{in}');
get(h,'Interpreter');
What's interesting is, if I modify one of the legend labels slightly, such as a backslash somewhere, some will properly subscript themselves, and the others won't. I'm trying this in both R2014b and R2015a.
This used to work before, I don't know if this is a weird glitch or something. I've read about people using interpreter to turn subscripting off, but I wasn't sure if it would apply in my case.
But what does
get(h,'Interpreter')
return? Is it tex or something else?
It returned tex at one point. It works one time and them the next time it doesn't work.
I just did it again and it doesn't work. But a few times before that it worked. doing this for titles isn't this tasking.
The get() function should always work. When get() does not work, what error message does get() throw?
No error message. When I apply get(h,'interpreter'), tex is returned, but while I get the correct result when I run the code on trial 1, trials 2-10 give me the underscores instead of the subscripts.
Can you attach ALL your code, because this is what I get when I try to run the snippet that you included:
Undefined function 'tf' for input arguments of type 'double'.
Error in test1 (line 10)
sys1 = tf(N1,D1);
clc
clear all
figure(1)
hold on
% e_1/e_in
N1 = (1);
D1 = [1.6e-5 1];
sys1 = tf(N1,D1);
% e_2/e_in
N2 = (2);
D2 = [1.6e-5 1];
sys2 = tf(N2,D2);
% e_out/e_in
N3 = (6.4e-4);
D3 = [5.12e-9 3.36e-4 1];
sys3 = tf(N3,D3);
bodemag(sys1,'-k',sys2,'--k',sys3,'-.k')
grid on
hold off
title('Bode Plot Evolution of Circuit Signal');
h=legend('e_1/e_{in}','e_2/e_{in}','e_{out}/e_{in}');
get(h,'Interpreter');
That's all of what I'm using.
It looks like you just posted the same code I told you did not work. The function or array tf is not defined for me. What is it? Maybe it's in some toolbox that you forgot to list in the Products section below? It's not in base MATLAB. Or maybe it's some array or function from some part of your code that you forgot to include. I don't know, but I can't run it because tf is not defined for me.
It's for a transfer function. I'm using 2014b or 2015a...
>> help tf tf Construct transfer function or convert to transfer function.
Construction:
SYS = tf(NUM,DEN) creates a continuous-time transfer function SYS with
numerator NUM and denominator DEN. SYS is an object of type tf when
NUM,DEN are numeric arrays, of type GENSS when NUM,DEN depend on tunable
parameters (see REALP and GENMAT), and of type USS when NUM,DEN are
uncertain (requires Robust Control Toolbox).
That is literally all and only all the code I used. Nothing more, nothing less.
I ran your code several times on 2014a and did not have a problem. You mentioned adding a backslash somewhere - that sounds like a bad idea, since backslashes have a special role in TeX.
Ya I abandoned that backslash idea for that same reason. I mean the ultimate question is--why when doing this for a title or axis works perfectly, but for the legend, it wants to play games like this?
I added the Robust Control Toolbox for you to the Products list below. I do not have that toolbox.
This is a long shot, but what answer do you get for
which legend
?
What is Robust Control Toolbox...do I have that? I am fairly novice at MATLAB, but for simple things like that I never had a problem with until now.
Andrew---for which legend:
C:\Program Files\MATLAB\R2015a\toolbox\matlab\scribe\legend.p
And, my subscripts appear perfectly. For this run...let me check again.
And when I run it again--back to square one :/
Actually, it might be the Control System Toolbox you're using: the functions tf and bodemag are both in it. Probably the reason ImageAnalyst couldn't run your code is that he doesn't have the toolbox.
Is it maybe the release I'm using...is this is a glitch for 2014b and later...I would download 2014a again but I have a feeling it isn't going to change anything.
Since I can't reproduce it in 2014a, my guess is that it is a bug introduced in 2014b. But that's all it is - a guess.
I may have another guess--at the end of the installation process it said I needed to download a Microsoft compiler add-on...but I didn't do that (yet). Is that maybe causing the trouble?
And, I looked into installing that, but its for Windows 7, and I have Windows 8...what should I do there?
I doubt it's the problem but you can get the Microsoft Visual C++ Redistributable Packages for Visual Studio 2013 here: http://www.microsoft.com/en-us/download/details.aspx?id=40784 It can't hurt to try.
I'm now convinced it is a bug in 2014b and later as I just downloaded 2014a and the subscripts work fine each and everytime. And that's without downloading those extra compilers.
Why did you get the old version? Since you're downloading another version why didn't you just get the latest one, R2015a???
That's where I ran into this problem at--I downloaded 2015a the day it came out. That version has this bug in it too. I just downloaded 2014a today to see if it had the same issue.
Well I have R2015a and I'm not seeing it, so it maybe something special to your computer. Can you make a generic version, without the tf function, to demonstrate the problem? I'll try it on my computer again.
Well now this is interesting--if I do a generic plot() function with 3 different curves, and create a legend with subscripts--the same ones as I am trying to do---it all works fine each and every time.
So, we can probably rule out the overall bug in the system...perhaps its just for the bodemag plot?
N/A
N/A 2015-3-15
编辑:N/A 2015-3-15
It has to be a bug with bode commands, as I made a new generic script--in fact I modified the one I used to test plot on, and for both bode and bodemag commands, the subscripts do not seem to work.
Edit: I have 2014a, 2014b, and 2015a all open at the same time and I ran the same code each three times in a row...2014a gives subscripts, while 2014b and 2015a do not...and this is just for the bode commands. Plot works fine with legend subscripts.
Though impractical, I tried this:
N1=(1);
D1=[4 2];
N2=(5);
D2=[4 5 6];
x=linspace(0,10,1000);
y=sin(x);
sys1=tf(N1,D1);
sys2=tf(N2,D2);
sys3=tf(N3,D3);
figure()
hold on
bode(sys1,'-k');
bode(sys2,'--k');
plot(x,y,'-.k');
legend('e_1/e_{in}','e_2/e_{in}','e_{out}/e_{in}');
And I got the figure attached/shown. The first two entries, no subscripts (from bode) and the third, subscripts (from plot). And this is all done in R2015a.
Curious! It looks like the culprit is in bode, but I don't think we'll be able to solve it in this forum. It's time to get Mathworks involved.
Ya that's unfortunately what I was thinking. I already submitted a bug request and linked this forum question to it...how is their reputation of responding to bug reports? Quickly or "eventually"?
If you have 2014b or 2015a and you run the above code, do you get what I get or does it all work out for you?
One more tiny question---does anyone have the link that appears at the end of the R2015a installation/download that directs you to install those extra compilers you can install to finish the entire installation process? I am using Windows 8.1, but if the link gives only Windows 7 compiler options, can I still download them or is there an alternate place for Windows 8. Or, are these not necessary to install at all?
I received a reply from MathWorks about this issue--here is what they said:
I understand you would like to add subscripted text to your legend entries. There is a bug in the way MATLAB R2014b and R2015a handle these legend entries. For example, suppose we create a bode plot with a subscripted legend entry:
>> figure;
>> sys = tf([1],[1]);
>> bode(sys);
and then create a legend:
>> h = legend('p_1');
This will incorrectly display the subscripted legend text. The current workaround is to reassign the text for the legend entry:
>> set(h,'string',{'p_1'});
Hope this is helpful to everyone...thanks again for everyone's input!
Thanks for letting us know the answer. It's good to know there is a workaround.
Thanks for sharing !

请先登录,再进行评论。

 采纳的回答

I'm beginning to think you should submit a bug report. However, maybe you'll get more consistent results with
h=legend('$e_1/e_{in}$','$e_2/e_{in}$','$e_{out}/e_{in}$');
set(h,'Interpreter','latex')
Or, for better style,
h=legend('$e_1/e_\mathrm{in}$','$e_2/e_\mathrm{in}$','$e_\mathrm{out}/e_\mathrm{in}$');

3 个评论

The Latex format works, but the subscripts are still there---how do I submit a bug report?
I know this is old, but since it's the first thing that appeared when I searched about it, here's for future references until they fix it. The bug is still here: in bodeplot/bodemag the LaTex interpreter breaks if 'complex' expressions with subscripts and superscripts are provided in the legend arguments.
%% Attempt with Tex, explicitly set for ease of reading
legend('W_{p1}^{-1}', 'W_{p2}^{-1}', 'W_{p3}^{-1}', 'Interpreter', 'tex')
%% First attempt with LaTex
legend('$W_{p1}^{-1}$', '$W_{p2}^{-1}$', '$W_{p3}^{-1}$', 'Interpreter', 'latex')
%% Second attempt with LaTex, following
% <https://www.mathworks.com/matlabcentral/answers/183015-subscripts-in-matlab-legends#comment_272533>
l = legend('$W_{p1}^{-1}$', '$W_{p2}^{-1}$', '$W_{p3}^{-1}$', 'Interpreter', 'latex')
set(l, 'string',{'$W_{p1}^{-1}$', '$W_{p2}^{-1}$', '$W_{p3}^{-1}$'})
%It seems that there's no need for line 35, since the assignment to l
%actually solves the problem and shows the correct legend
%But then if the legend contains something more "complex" than only
%subscripts and superscripts, it breaks again
l = legend('1+ $W_{p1}^{-1}$', '$1/W_{p2}^{-1}$', '$1-W_{p3}$', 'Interpreter', 'latex')
Here's what the Techical Support told me:
"This appears to me like a bug, since the same syntax that you used when creating the legend in a standard plot with 3 lines works as expected, while when using bodeplot or bodemag a formatting issue is encountered. I will inform our development team right away so that they can consider to fix this for a future release."

请先登录,再进行评论。

更多回答(3 个)

Ankush Chakrabarty
Ankush Chakrabarty 2017-12-11

6 个投票

In MATLAB R2016a, the fix seems to be adding a "\_" instead of a "_"
e.g. h = legend('$G\_{\mathrm{a}}$', '$G\_{\mathrm{b}}$'); set(h, 'Interpreter', 'latex');
TRY this
clc
clear all
a=-360:1:360;
b=sind(a);
c=cosd(a);
plot(a,b)
hold on
plot (a,c)
xlabel('angle in degrees')
ylabel('value of funtion')
title('sin and cos curve')
legend('sin curve','cos curve')
hold off
>> doc interpreter
Also, backslash is a special character that sometimes modifies what comes after it, so it's not surprising you get unexpected behavior if you don't know that.

2 个评论

Nope, not working. The legend still reads "e_out..." and so forth.
Here's what I'm faced with now: It will properly do the subscripts, but at any random run of the script. I just did it now, and it worked. Ran the same thing two seconds later...back to not working.

请先登录,再进行评论。

类别

Community Treasure Hunt

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

Start Hunting!

Translated by