How to locate X value for a given Y value

17 ビュー (過去 30 日間)
David Harra
David Harra 2023 年 2 月 1 日
回答済み: Star Strider 2023 年 2 月 2 日
I have Data which is Time along the x-axis and amplitude along the y -axis.
I want to know the exact time at a given value of amplitude. Its probably just one line of code but I can't seem to figure it out.
Any help would be appreciated.

採用された回答

Star Strider
Star Strider 2023 年 2 月 2 日
In the absence of the data, getting an independent variable valuex ‘x’ from an dependent variable value ‘y’ is relatively straightforward.
Try soemthing like this —
x = linspace(0, 20, 201);
y = besselj(0, x);
yval = linspace(-0.2, 0.2, 6);
for k1 = 1:numel(yval)
zxi = find(diff(sign(y-yval(k1))));
for k2 = 1:numel(zxi)
idxrng = max(1,zxi(k2)-1) : min(numel(x), zxi(k2)+1);
xv(k1,k2) = interp1(y(idxrng), x(idxrng), yval(k1));
end
end
for k1 = 1:size(xv,1)
xv(k1,xv(k1,:)==0) = NaN;
end
figure
plot(x, y)
hold on
for k1 = 1:size(xv,1)
plot(xv(k1,:), ones(size(xv(k1,:)))*yval(k1), 's')
end
yline(yval, ':')
hold off
grid
xlabel('X')
ylabel('Y')
.

その他の回答 (1 件)

the cyclist
the cyclist 2023 年 2 月 1 日
編集済み: the cyclist 2023 年 2 月 1 日
It would be helpful if you uploaded the data. You can use the paper clip icon in the INSERT section of the toolbar.
It seems like you need either
% The exact value appears in the amplitude vector, and want to find what Time it occurs
Time(amplitude == value) % be careful here if you are comparing floating point values
or
% The exact value does not appear in the amplitude vector, so you want the
% first Time where you surpass that value
Time(find(amplitude>value,1)) % this assume the values of amplitude and time are sorted appropriately
You might also want to interpolate between times, but be more exact. Then you'll need the interp1 function.
  2 件のコメント
David Harra
David Harra 2023 年 2 月 2 日
Thanks for the reply
The type of data I have won't let me attach. My file is a .flxhst file and I use a function to read this data to obtain my time and amplitude data from a simulation.
I tried the code and it seems to give me something close to what I need but I wouild be interested in learning how to apply the interpolate function you mentioned. I have little room for error so may be more efficient
Time(find(amplitude>value,1)
the cyclist
the cyclist 2023 年 2 月 2 日
You can't upload just your Time and amplitude vectors in a MAT file?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTime Series Collections についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by