Subscripts in MATLAB Legends
Show older comments
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 Comments
Geoff Hayes
on 14 Mar 2015
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.)
N/A
on 14 Mar 2015
N/A
on 14 Mar 2015
Geoff Hayes
on 14 Mar 2015
But what does
get(h,'Interpreter')
return? Is it tex or something else?
N/A
on 14 Mar 2015
N/A
on 14 Mar 2015
Image Analyst
on 14 Mar 2015
The get() function should always work. When get() does not work, what error message does get() throw?
N/A
on 14 Mar 2015
Image Analyst
on 14 Mar 2015
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);
N/A
on 14 Mar 2015
N/A
on 14 Mar 2015
Image Analyst
on 14 Mar 2015
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.
N/A
on 14 Mar 2015
N/A
on 14 Mar 2015
N/A
on 14 Mar 2015
Andrew Newell
on 14 Mar 2015
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.
N/A
on 14 Mar 2015
Mark Hayworth
on 14 Mar 2015
I added the Robust Control Toolbox for you to the Products list below. I do not have that toolbox.
Andrew Newell
on 14 Mar 2015
This is a long shot, but what answer do you get for
which legend
?
N/A
on 14 Mar 2015
N/A
on 14 Mar 2015
N/A
on 14 Mar 2015
Andrew Newell
on 14 Mar 2015
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.
N/A
on 14 Mar 2015
Andrew Newell
on 14 Mar 2015
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.
N/A
on 14 Mar 2015
Image Analyst
on 14 Mar 2015
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.
N/A
on 14 Mar 2015
Image Analyst
on 14 Mar 2015
Why did you get the old version? Since you're downloading another version why didn't you just get the latest one, R2015a???
N/A
on 14 Mar 2015
Image Analyst
on 15 Mar 2015
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.
N/A
on 15 Mar 2015
Andrew Newell
on 15 Mar 2015
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.
N/A
on 15 Mar 2015
N/A
on 15 Mar 2015
N/A
on 16 Mar 2015
Andrew Newell
on 16 Mar 2015
Thanks for letting us know the answer. It's good to know there is a workaround.
Lucas Teixeira
on 10 May 2020
Thanks for sharing !
Accepted Answer
More Answers (3)
Ankush Chakrabarty
on 11 Dec 2017
6 votes
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');
1 Comment
Arkadeb Sengupta
on 27 Sep 2020
This one worked in 2018b too. The other fixes didn't.
Muhammad Sarfraz Mirza
on 5 Oct 2021
1 vote
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
Image Analyst
on 14 Mar 2015
>> 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.
Categories
Find more on Legend 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!