MATLAB Answers

Creating intersection lines in a semilogx graph

13 views (last 30 days)
Eric on 23 Jan 2011
I have the following MATLAB code to create a Bode plot from measured data:
function bodeFromMeasure(Freq_Hz, Mag_V)
%bodeFromMeasure(Freq_Hz, Mag_V)
% Freq_Hz: vector of unconverted x data
% Mag_V: vector of unconverted y data
% Convert Variables
MagdB = 20.*log(Mag_V);
Freq_w = 2.*pi.*Freq_Hz;
% Create Bode Plot
bodePlot = figure('XVisual',...
'0x24 (TrueColor, depth 24, RGB mask 0xff0000 0xff00 0x00ff)',...
'Name','Lab 1 - Bode Plot');
% Create axes
axes1 = axes('Parent',bodePlot,'YGrid','on','XScale','log','XMinorTick','on',...
xlim(axes1,[0 150000000]);
% Create semilogx
% Create xlabel
xlabel('Frequency (rad/sec)','FontSize',11);
% Create ylabel
ylabel('Magnitude (dB)','FontSize',11);
% Create title
title('Bode Plot','FontWeight','bold','FontSize',14);
% Create lines
I would like to do the following 4 things through code, unfortunately I do not have the knowledge to do them:
  1. Create a horizontal dashed line at y=0 to intersect the graphed curve
  2. Create a vertical dashed line that intersects the intersection created between the horizontal line and the graphed curve
  3. Place a datatip at the y=0 point on the graphed curve
  4. Automatically set the upper limit of the x-axis range to the last value in the x-axis vector data array
I am not very experienced in MATLAB and have looked through forums and function syntax and still cannot accomplish these things. Can anyone more experienced in the program lend a hand?
Thanks in advance for any help.
  1 Comment
Kenneth Eaton
Kenneth Eaton on 24 Jan 2011
What range of values (i.e. min and max) do your inputs Freq_Hz and Mag_V have?

Sign in to comment.

Answers (2)

Kenneth Eaton
Kenneth Eaton on 23 Jan 2011
Addressing the first 3 points, it sounds like you want to create a datatip with crosshairs on one point of your curve. You can place datatips using the Data Cursor tool. These data tips don't have crosshairs by default, so you can either plot them yourself, or use my solution to this other question to attach crosshairs to the datatip that automatically update when the datatip is moved.
Addressing point 4, you can change the x axis limit just like you did using the function XLIM, but specify the maximum value of the range using the function MAX like so:
xlim(axes1,[0 max(Freq_w)]);

Eric on 23 Jan 2011
Using your solution for the crosshairs, it only creates a vertical line intersecting the datatip in my graph. Also, is it not possible to create the datatip programmatically at the point y=0?

Community Treasure Hunt

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

Start Hunting!