Subscripts in MATLAB Legends

148 ビュー (過去 30 日間)
N/A
N/A 2015 年 3 月 13 日
回答済み: Muhammad Sarfraz Mirza 2021 年 10 月 5 日
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 件のコメント
Andrew Newell
Andrew Newell 2015 年 3 月 16 日
Thanks for letting us know the answer. It's good to know there is a workaround.
Lucas Teixeira
Lucas Teixeira 2020 年 5 月 10 日
Thanks for sharing !

サインインしてコメントする。

採用された回答

Andrew Newell
Andrew Newell 2015 年 3 月 14 日
編集済み: Andrew Newell 2015 年 3 月 14 日
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 件のコメント
Andrew Newell
Andrew Newell 2015 年 3 月 14 日
Simone Vollaro
Simone Vollaro 2020 年 6 月 24 日
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 日
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 件のコメント
Arkadeb Sengupta
Arkadeb Sengupta 2020 年 9 月 27 日
This one worked in 2018b too. The other fixes didn't.

サインインしてコメントする。


Muhammad Sarfraz Mirza
Muhammad Sarfraz Mirza 2021 年 10 月 5 日
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
Image Analyst 2015 年 3 月 14 日
>> 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 件のコメント
N/A
N/A 2015 年 3 月 14 日
Nope, not working. The legend still reads "e_out..." and so forth.
N/A
N/A 2015 年 3 月 14 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by