Legends using bodeplot with latex interpretation

14 views (last 30 days)
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");):
"Error using legend (line 176). First argument must be text."
I appreciate any input.
Marcus

Accepted Answer

Cris LaPierre
Cris LaPierre on 27 Mar 2025
Something about how bodeplot now creates a chart object has made it difficult to set the interpreter property of the legend programmatically. I found a workaround, but it is manual.
The bodeplot chart object has a LegendVisible property. By default, this uses the variable names. You can set this property to 'on' and update the names used in the legend by calling the legend function in your code. You cannot, however, set the interpreter.
load iddata1
sys1 = n4sid(z1,2);
sys2 = n4sid(z1,6);
names = {'$G_{11}$', '$G_{12}$'};
bodeplot(sys1,'r',sys2,'b')
legend(names)
Once the figure has been created, open the property inspector (if in a live script, first open it in a new figure window) and select the legend box (#4 below. Note the breadcrumbs at the top of the Property inspector). In the 'Labels' section
  • select String to view the labels. Remove the backslashes that have been added to the names.
  • Set the Interpreter to 'latex'
Now close the propery inspector. The legend text should appear as intended.
I've reported this internally.
  1 Comment
Marcus
Marcus on 30 Mar 2025
Thank you for reporting it, hopefully a fix will come through at some point. I'll do as you suggested until that time.

Sign in to comment.

More Answers (1)

dpb
dpb on 25 Mar 2025
Edited: dpb on 25 Mar 2025
which -all bodeplot
/MATLAB/toolbox/shared/controllib/engine/@DynamicSystem/bodeplot.m % DynamicSystem method
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');
Warning: Ignoring extra legend entries.
figure
bp = bodeplot(sys_p,sys_np,w);
bp.PhaseMatchingEnabled = "on";
grid on
hLg=legend(names(1),names(2));
hLg.Interpreter
ans = 'tex'
hLg.Interpreter='latex'
hLg =
Legend ($G\_{11}$, $G\_{12}$) with properties: String: {'$G\_{11}$' '$G\_{12}$'} Location: 'northeast' Orientation: 'vertical' FontSize: 8.1000 Position: [0.8298 0.8284 0.1487 0.0857] Units: 'normalized' Use GET to show all properties
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,'$','')
names = 1x4 cell array
{'G_{11}'} {'G_{12}'} {'G_{21}'} {'G_{22}'}
hLG=legend(names(1),names(2));
hLG.Interpreter
ans = 'tex'
Except TeX is broke, too. it's definitely buggy.
  3 Comments
Marcus
Marcus on 27 Mar 2025
Hi @dpb,
Thank you for taking the time to look into this and respond, it is of course a little frustrating that there appears not to be a workaround. But thanks anyway!
dpb
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...

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!