MATLAB: Display the x value of a graph knowing the y value

graphmatrix

Hi, I have a graph y(x) and I want to know when y has a value which is the correspondent value of x. How can I do it? I can't use the data cursor because I have to do it hundreds of times.
Is there a way to input the value of y and obtain the value of x?
Thanks

Best Answer

Supposing you had done
plot(x,y)
and that the value you are looking for is ProbeY (an arbitrary variable name)
[ydist, yidx] = sort(abs(y - ProbeY));
Then
x(yidx(1))
is the x for which y(x) is the closest to ProbeY.
Sometimes, though, you only want locations where y(x) is at most ProbeY
ylocs = find(ProbeY >= y);
[ydist, ylocsidx] = sort(ProbeY(ylocs)-y);
closestyidx = ylocs(ylocsidx(1));
and then x(closestyidx) is the x for which y(x) is closest to but does not exceed ProbeY.