BodePlot: Plotting straight lines on the Mag and Phase plots. Can access the phase plot, to do so, but not the mag plot
Mostra commenti meno recenti
Hi there
I was customising the standard MATLAB Bode plot to make the gridlines, major and minor bold,
change the x and y label size and bold them and also customise the axis.
Then I tried to plot the straight line approximation, but I can only seem to access the phase plot, and not the magnitude plot.
For the two axes, ax(1) and ax(2), I have called them mag_ax and phase_ax.
I can access the other properties fine, but when I tries to add the lines, my code doesn't recognise the mag_ax.
Any advice greatly appreciated
close all
s = tf('s');
G_1 = tf(1/(0.1*s + 1));
figure('WindowState','maximized');
gg = bodeplot(G_1);
title('Bode plot of $\frac{1}{0.1s+1}$',...
'Interpreter','latex','FontSize',18,'FontWeight','bold');
gg.Responses.Color = "blue";
gg.Responses.LineWidth = 2;
gg.XLabel.FontWeight = "bold";
% Get the handles of the axes
ax = findobj(gcf,'type','axes');
mag_ax = ax(1);
phase_ax = ax(2);
mag_ax.XLim = [0.1 1000];
mag_ax.YLim= [-60 +60];
phase_ax.XLim = [0.1 1000];
phase_ax.YLim= [-90 +90];
phase_ax.YTick = (-90:45:90);
grid on
grid minor
phase_ax.GridLineWidth = 1;
mag_ax.GridLineWidth = 1;
phase_ax.MinorGridLineStyle = '-';
mag_ax.MinorGridLineStyle = '-';
phase_ax.MinorGridLineWidth = 0.75;
mag_ax.MinorGridLineWidth = 0.75;
phase_ax.MinorGridAlpha = 0.5;
mag_ax.MinorGridAlpha = 0.5;
phase_ax.GridLineWidth = 1;
mag_ax.GridLineWidth = 1;
phase_ax.GridAlpha = 1;
mag_ax.GridAlpha = 1;
mag_ax.YLabel.FontWeight = "bold";
mag_ax.YLabel.FontSize = 12;
phase_ax.YLabel.FontWeight = "bold";
phase_ax.YLabel.FontSize = 12;
for k = 1:length(phase_ax)
xticks(phase_ax(k), [0.1 1 10 100 1000]); % Set desired tick positions
end
for k = 1:length(phase_ax)
phase_ax(k).XTickLabel = arrayfun(@(x) sprintf('%.1f', x), phase_ax(k).XTick, 'UniformOutput', false);
end
set(phase_ax,'fontweight','bold')
set(mag_ax,'fontweight','bold')
gg.Responses.Visible = 'on';
gg.Responses.Color = 'blue';
mag_ax.NextPlot = 'add';
line([0.1 10.0],[0 0],'linewidth',2,'color','red');
line([10.0 1000],[0 -40],'linewidth',2,'color','red');
phase_ax.NextPlot = 'add';
line([0.1 1.0],[0 0],'linewidth',2,'color','red');
line([1.0 100],[0 -90],'linewidth',2,'color','red');
line([100 1000],[-90 -90],'linewidth',2,'color','red');
Risposta accettata
Più risposte (1)
I would recommend avoiding the use of "findobj" in this manner. Utilizing the chart API is the preferred (and more supported!) approach to this kind of problem.
Here is my take on generating your desired plot using only the chart API. The convience commands you used like "grid" and "title" are supported, but I'm more of a fan of directly interacting with the chart handle.
G_1 = tf(1,[0.1 1]);
gg = bodeplot(G_1);
% Style labels
gg.Title.String = "Bode plot of $\frac{1}{0.1s+1}$";
gg.Title.Interpreter = "latex";
gg.Title.FontSize = 18;
gg.Title.FontWeight = "bold";
gg.XLabel.FontWeight = "bold";
gg.YLabel.FontWeight = "bold";
gg.YLabel.FontSize = 12;
gg.XLimits = [0.1 1000];
gg.YLimits = {[-60 60];[-90 90]};
% Style axes
gg.AxesStyle.GridVisible = "on";
gg.AxesStyle.GridLineWidth = 1;
gg.AxesStyle.GridAlpha = 1;
gg.AxesStyle.MinorGridVisible = "on";
gg.AxesStyle.MinorGridLineWidth = 0.75;
gg.AxesStyle.MinorGridLineStyle = "-";
gg.AxesStyle.MinorGridAlpha = 0.5;
gg.AxesStyle.FontWeight = "bold";
% Style response
gg.Responses(1).LineWidth = 2;
% Add extra data
gg.NextPlot = "add";
gg.DataAxes = [1 1]; %target mag axes
line([0.1 10.0],[0 0],Linewidth=2,SeriesIndex=2);
line([10.0 1000],[0 -40],Linewidth=2,SeriesIndex=2);
gg.DataAxes = [2 1]; %target phase axes
line([0.1 1.0],[0 0],Linewidth=2,SeriesIndex=2);
line([1.0 100],[0 -90],Linewidth=2,SeriesIndex=2);
line([100 1000],[-90 -90],Linewidth=2,SeriesIndex=2);
3 Commenti
Paul
il 15 Apr 2026
Just want to point out that the DataAxes property only arrived in 2025a.
Gerard Nagle
il 16 Apr 2026
Andrew Ouellette
il 16 Apr 2026
As Paul pointed out, DataAxes is new as of R2025a. The entire chart API is new as of R2024b!
With the newly released R2026a, you can perform the response styling in the "bodeplot" function call:
gg = bodeplot(G_1,SeriesIndex=1,LineWidth=2)
Categorie
Scopri di più su Specifying Target for Graphics Output in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



