x and y- line until point

111 views (last 30 days)
Robin Kleemann
Robin Kleemann on 7 Oct 2022
Commented: Star Strider on 10 Oct 2022
Hello, I want to create a diagram which looks exactly like this. Now I want to have a dashed line in x and y direction up to this point.
with the command xline() and yline() I can only draw lines over the whole plot, does anyone have an idea how to limit this?
Thanks for your help!

Accepted Answer

Star Strider
Star Strider on 7 Oct 2022
Illustrating the approach —
x = linspace(0, 5); % Create Data
y = sin(2*pi*x/2.5) + 1.05*x; % Create Data
Rmin = 3.27; % Define Value
ix = find(diff(sign(y-Rmin))); % Find Approximate Index
idxrng = max(1,ix-1) : min(numel(x),ix+1); % Define Index Range
xRmin = interp1(y(idxrng),x(idxrng), Rmin); % X-Coordinate Of Intercept
yRmin = interp1(x(idxrng),y(idxrng), xRmin); % Y-Coordinate Of Intercept
figure
plot(x, y)
hold on
plot([0 xRmin], [1 1]*Rmin, '--r') % Horizontal Dashed Line
plot([1 1]*xRmin, [0 1]*yRmin, '--r') % Vertical Dashed Line
plot(xRmin, yRmin, 'or') % Marker
hold off
grid
text(0,yRmin, 'yR_{min}', 'Horiz','right', 'Vert','middle') % Horizontal Line Axis Label
text(xRmin,0, 'xR_{min}', 'Horiz','center', 'Vert','top') % Vertical Line Axis Label
If you have a vector of the relevant R values, you can put the interp1 calls in a loop and return a vector of the appropriate coordinates. Depending on the initial R values, order the interp1 calls to find the appropriate missing values. The dashed line plot calls can also be in a loop. With respect to , there would be two intercepts of the curve for the time values, so you would have to choose the first one (with the lowest index). The others should be striaghtforward.
If you are starting with the time values in a vector and have the data for the curve, this is much simpler and only requires a single interp1 call to get all the R values.
.
  2 Comments
Robin Kleemann
Robin Kleemann on 10 Oct 2022
perfect, thank you!
Star Strider
Star Strider on 10 Oct 2022
As always, my pleasure!

Sign in to comment.

More Answers (1)

Jiri Hajek
Jiri Hajek on 7 Oct 2022
You will neet to plot the auxiliary lines separately - first plot the graph, then use the hold function and last plot all the lines you need. To plot a straight line, you need just the starting and end point.
xline and yline are ment to always span the whole plot area, which simplifies inputs for their plotting.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!