Underscore doesn't work for step legend

73 views (last 30 days)
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 Comments
Adam Danz
Adam Danz on 3 May 2019
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);
190503 174156-Figure 1.jpg
Vitor Sternlicht
Vitor Sternlicht on 3 May 2019
Yep, it works fine with plot. Not with step though. I need it to work with these:
plot(t_exp,omega_exp,'red'); % works
hold on
plot(t_exp,theta_exp,'blue'); % works
hold on
step(12*Go); % doesn't work
hold on
step(12*Gt); % doesn't work
For some reason Matlab messes up the legends after steps.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 3 May 2019
Edited: Adam Danz on 28 Aug 2020
The escape character (\) is mistakenly added in legend() > setDisplayNames() even though the latex interpreter is correctly assigned. This evokes updateGroupInfo() which adds the escape character. Even if you assign the string to the DisplayName property prior to calling legend(), it immediately addes the escape character. However, the string is correctly assign in latex (or tex) format if the assignment is done after the legend is created. This is because you are only altering the legend string and therefore bypassing the updateGroupInfo() function.
Links to other threads in this forum that have revealed this problem with other plotting functions:
Given that, here are two work-arounds. Also see per isakson's solution below for a 3rd alternative.
Workaround 1
Call the legend twice.
% reproducible example
figure();
s = tf('s');
G = exp(-s) * (0.8*s^2+s+2)/(s^2+s);
T = feedback(ss(G),1);
step(T)
legend({'$\omega_{modelo} (rad/s)$'},...
'Interpreter','latex','Location','southeast','FontSize',16);
legend({'$\omega_{modelo} (rad/s)$'})
Workaround 2
Assign the DisplayName property after creating the legend.
figure
s = tf('s');
G = exp(-s) * (0.8*s^2+s+2)/(s^2+s);
T = feedback(ss(G),1);
step(T);
legend('Interpreter', 'latex')
axisObjs = get(gca, 'Children');
set(axisObjs(1), 'DisplayName', '$\omega_{modelo} (rad/s)$')

More Answers (1)

per isakson
per isakson on 3 May 2019
Edited: per isakson on 4 May 2019
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
And in accordance with Adam Danz replace legend(___) by
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 Comments
Walter Roberson
Walter Roberson on 3 May 2019
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!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!