How to find a y value given the x on a plot?

Hello!
My current problem is I'm trying to achieve a plot like this:
But all I have is the blue plot and the x's of the values I want to scatter plot on the line. I was wondering if there was a way to get the y values from the plot.
I tried idexing the blue signal with che x's, but it gives me an ''index must be positive integers'' error.
Can anyone help me with that?

 Accepted Answer

Use interpolation, specifically interp1 here.
Without your data, an illustration would be something like this —
x = linspace(-5, 5, 50);
y = sinc(x);
xv = 10*rand(3,1)-5 % Arbitrary 'x' Values
xv = 3×1
2.0330 -0.3249 -2.3227
yv = interp1(x, y, xv, 'linear') % Corresp[inding 'y' Values
yv = 3×1
0.0129 0.8317 0.1136
figure
plot(x, y)
grid
hold on
plot(xv, yv, 'p')
hold off
.

2 Comments

Thank you so much!
I had came across this type of solution but couldn't seem to undestand how to implement it.
I finally managed thanks to you.
Have a wonderful day!
As alkways, my pleasure!
I appreciate your compliment.
You have a wonderful day, too!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!