Calculating a time duration from the graph

6 ビュー (過去 30 日間)
Mridul Garg
Mridul Garg 2016 年 6 月 29 日
回答済み: Aleksandra Ksiezyk 2020 年 1 月 20 日
Hello
I am interested in finding out the time between two points in a graph (x-axis is the time, y-axis is the quantity of interest). For testing my logic, I'm using a sinusoidal wave to see if I get the correct responses. In the sinusoidal wave defined below, I'm interested in calculating the starting and ending x-coordinate when the y-axis value is less than 0.4.
t= [0:0.1:2*pi];
a=sin(t);
figure;
h=plot(t,a);
for i=1:max(t)
y=get(h,'ydata');
if a < 0.4
start=get(h,'xdata');
if a > 0.4
fin=get(h,'xdata');
end
end
This is an infinite loop, which doesn't give any output. I'm not really familiar with matlab and am not able to figure out how to get the start and fin values above.
Any help is appreciated.
Thanks!

採用された回答

KSSV
KSSV 2016 年 6 月 29 日
t= [0:0.1:2*pi];
a=sin(t);
figure;
h=plot(t,a);
% Get y data
y=get(h,'ydata');
% get x data
x=get(h,'xdata');
% find indices less then 0.4 in y
idx = find(y<0.4) ;
% get values of y less then 0.4
y1 = y(idx) ;
% get time for respective y values
x1 = x(idx) ;
% min time of 0.4
xmin = min(x1)
% max time of 0.4
xmax = max(x)
  2 件のコメント
Mridul Garg
Mridul Garg 2016 年 6 月 29 日
Thank you so much for this. However, I want to generalize this function, in the sense that if there are two instances of the curve going below 0.4 and coming up above 0.4, I would want the start and end indices of both instances?
KSSV
KSSV 2016 年 6 月 29 日
Can be done.....doc find..

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

その他の回答 (1 件)

Aleksandra Ksiezyk
Aleksandra Ksiezyk 2020 年 1 月 20 日
hallo,
I came across this post because I am looking for something similar.
do you know how one can calculate the length of the x axis (or a curve) for a given condition? e.g. idx = find (y <0.4 & y> 0.3). I have a plot describing the temperature of time (24h) and I would like to calculate how much time during the day the temperature is from 17.5 to 18.5 C?
cheers,
Ola

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by