Legends using bodeplot with latex interpretation
Show older comments
Hi, I am trying to write code for plotting frequency responses and cannot get the legend to accept latex formatting. I know there are issues with all of these signal processing tools and stuff like legends, but if I open the figure in a new window and manually adjust the legend in the property editor then it is possible. So there must be a work around. Here is one of the many iterations I have used, in this version the legend appears, but as soon as I add interpreter to the command then I get errors such as seen below:
G1 = G(1,1);
G2 = G(1,2);
G3 = G(2,1);
G4 = G(2,2);
names = {'$G_{11}$', '$G_{12}$', '$G_{21}$', '$G_{22}$'};
LegendValues = string(names);
bp = bodeplot(G1, G2, G3, G4);
opts = bodeoptions;
opts.Title.String = ''; % Disable title
opts.XLabel.Interpreter = 'latex';
opts.XLabel.FontSize = 12;
opts.YLabel.Interpreter = 'latex';
opts.YLabel.FontSize = 12;
opts.PhaseVisible = 'off'; % Hide phase plot
opts.Grid = 'on'; % Enable grid
setoptions(bp, opts);
legend(LegendValues);
Error if I add interpreter (legend(LegendValues, "Interpreter","latex");):
I appreciate any input.
Marcus
Accepted Answer
More Answers (1)
which -all bodeplot
load iddata2 z2;
w = linspace(0,10*pi,128);
sys_np = spa(z2,[],w);
sys_p = tfest(z2,2);
bp = bodeplot(sys_p,sys_np,w);
bp.PhaseMatchingEnabled = "on";
grid on
names = {'$G_{11}$', '$G_{12}$', '$G_{21}$', '$G_{22}$'};
%legend('Parametric Model','Non-Parametric model');
legend(names(1),names(2),'interpreter','latex');
figure
bp = bodeplot(sys_p,sys_np,w);
bp.PhaseMatchingEnabled = "on";
grid on
hLg=legend(names(1),names(2));
hLg.Interpreter
hLg.Interpreter='latex'
It appears that legend with the bodeplot is neutered/broken...it is interpreting the 'Interpreter' named parameter as an extra legend and errors there. Setting the interpreter property changes the internal state and doesn't error, but doesn't render the subscripts correctly.
A test of an ordinary plot into a plain axes shows the subscripted G labels as expected.
I think it's a bug...
ADDENDUM
Probably the best workaround for now would be to go back to TeX but just use its syntax for underscore. Not quite as pretty, perhaps, but you might find a different font that would approximate...
figure
bp = bodeplot(sys_p,sys_np,w);
bp.PhaseMatchingEnabled = "on";
grid on
names=replace(names,'$','')
hLG=legend(names(1),names(2));
hLG.Interpreter
Except TeX is broke, too. it's definitely buggy.
3 Comments
subplot(2,1,1)
plot(randn(4))
names = {'$G_{11}$', '$G_{12}$', '$G_{21}$', '$G_{22}$'};
legend(names);
subplot(2,1,2)
plot(randn(4))
legend(names,'interpreter','latex');
A regular axes works as expected...
Marcus
on 27 Mar 2025
dpb
on 27 Mar 2025
I don't have the TB to try locally and haven't tried here...the one possible alternative might be if bodeplot is not too belligerent and would let you put another axes on top in which to write the legend...many of the new specialized plots don't allow for such, however...
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!




