How to add legend to both amplitude and phase plot of bodeplot

39 views (last 30 days)
Hello,
I have a simple transfer function, and i am using bodeplot to display amplitude and phase. However when i add legend in script, it is only added to the phase plot, is there anyway to add legend to amplitude also?
Also, the strange point is, the curve thickness is not shown in the legend, which is not the case like legend for normal plot command.
I am using matlab 2018a.
Thanks for your help.

Answers (1)

Curtis Plude
Curtis Plude on 14 Aug 2023
(I'm using MATLAB R2020b)
After plotting using the bodeplot function, retrieve a list of the figure's axes, and use the axes' handles to set the legends:
bodeplot(tf1,tf2);
ax = findall(gcf,'type','axes');
legend(ax(3),'tf1','tf2'); % ax(3) is the axes for the Magnitude plot
legend(ax(2),'tf1','tf2'); % ax(2) is the axes for the Phase plot
There seems to be only one legend object for the bodeplot. Consequently, if both axes are displaying a legend, the labels will be those specified last (either by calling the legend function, or by directly setting the .String property of the legend in either axes).
Note that ax(1) is the "Bode Diagram" axes, which is not visible in the figure except for the title string "Bode Diagram".

Community Treasure Hunt

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

Start Hunting!