MATLAB: Finding x for when y equals a certain value

xy

My current problem is that I have a huge set of data for a pressure rise caused by a train entering the tunnel. I would like to find the values of x whenever the pressure rises crosses a pressure threshold and would like to be able to work out the difference between the pair of x's (should only be one pair)

Best Answer

Say x is your x-vector, and I'll let y be your pressure readings, would the following work? I'll make the threshold 2 in this example, but obviously you have to use your value.
y = abs(randn(100,1));
x = 0:99;
index1 = find(y>2,1,'first');
index2 = find(y(index1+1:end)<2,1,'first');
index2 = index2+index1;
x(index2)-x(index1)