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

4 views (last 30 days)
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

Star Strider
Star Strider on 29 May 2021
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
Elena Dipalma
Elena Dipalma on 2 Jun 2021
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!
Star Strider
Star Strider on 2 Jun 2021
As alkways, my pleasure!
I appreciate your compliment.
You have a wonderful day, too!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!