Underscore doesn't work for step legend
이전 댓글 표시
I'm trying to plot two step responses alongside with experimental results. Plot looks okay, but for some reason Matlab inserts a backslash before underscore for the step legend. How do I fix the legend?
\omega_{experimental} and \theta_{experimental} work
\omega_{modelo} and \theta_{modelo} don't
f2 = figure('DefaultAxesFontSize', 14);
plot(t_exp,omega_exp,'red');
hold on
plot(t_exp,theta_exp,'blue');
hold on
step(12*Go);
hold on
step(12*Gt);
title('Comparação do modelo obtido com a resposta experimental')
xlabel('Tempo (segundos)')
ylabel(' ')
legend({'$\omega_{experimental} (rad/s)$','$\theta_{experimental} (rad)$',...
'$\omega_{modelo} (rad/s)$','$\theta_{modelo} (rad)$'},...
'Interpreter','latex','Location','southeast','FontSize',16);
set(gca,'XLim',[0 1])
This is what I'm getting:

This is the backslash that Matlab inserts for some reason (seen by double-clicking the plot legend):

Full code is attached. Thanks in advance!
댓글 수: 2
Adam Danz
2019년 5월 3일
Works fine for me (copied your call to legend)
figure
plot(rand(5,4))
legend({'$\omega_{experimental} (rad/s)$','$\theta_{experimental} (rad)$',...
'$\omega_{modelo} (rad/s)$','$\theta_{modelo} (rad)$'},...
'Interpreter','latex','Location','southeast','FontSize',16);

Vitor Sternlicht
2019년 5월 3일
채택된 답변
추가 답변 (1개)
per isakson
2019년 5월 3일
편집: per isakson
2019년 5월 4일
With R2018b. Deep inside and after too many if-statements to keep track of a backslash is added.
C:\Program Files\MATLAB\R2018b\toolbox\shared\controllib\graphics\@wavepack\@waveform\updateGroupInfo.m
line 19-21
if ~this.LegendSubsriptsEnabled
dispname = strrep(dispname,'_','\_');
end
C:\Program Files\MATLAB\R2018b\toolbox\matlab\scribe\legend.m

Why Line and Group, respectively?
The order of the text-arguments in the call of legend() matters

lh = legend({
'$\omega_{experimental} (rad/s)$' ...
, '$\theta_{experimental} (rad)$' ...
, '$\omega_{modelo} (rad/s)$' ...
, '$\theta_{modelo} (rad)$' } ...
, 'Interpreter','latex','Location','southeast','FontSize',16);
lh.String{3} = '$\omega_{modelo} (rad/s)$';
lh.String{4} = '$\theta_{modelo} (rad/s)$';
lh.String{4} = '$\theta_{modelo} (rad/s)$';
String(4) twice is not a copy&paste error!
댓글 수: 2
Adam Danz
2019년 5월 3일
+1
same in r2019a
Walter Roberson
2019년 5월 3일
Good tracking! I had just traced it down to that same assignment in legend.m, but had not at all noticed the listener that triggers the behaviour you noticed!
카테고리
도움말 센터 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!