Writing a conditional statement for timeseries data
11 ビュー (過去 30 日間)
古いコメントを表示
Hi,
In the code attached, I want to write a conditional statement based on datetime and sunrise sunset time. Sunrise is the sunrise time as character vector, sunrise_new is the sunrise time after converting it to HHMM format using datenum (but i am not sure which one to use). I want to figure out if it's a day or night based on datetime and sunrise sunset data. My code should read like:
If HH:MM value in DateTime is > sunrise & < sunset
time = 1
If HH:MM value in DateTime < sunrise & > sunset
time = 0
end
I don't know how to properly write conditional statement in matlab yet.
回答 (1 件)
Rohit
2022 年 8 月 22 日
Hi,
In order to compare the times, you can use the "datenum()" function as shown in the below example:
% Getting current time
now=datetime('now')
% Converting datetime to serial date number
format long
hour=now.Hour;
minn=now.Minute;
time_to_check=datenum(hour+":"+minn,'HH:MM')
sunrise = datenum('07:36', 'HH:MM' );
sunset = datenum('17:46', 'HH:MM' ) ;
% Conditional statement to figure out if it's a day or night
if time_to_check<sunrise || time_to_check>sunset
time=0;
else
time=1;
end
You can refer to the below Documentation link for "datenum" function along with the MATLAB Answers link:
2 件のコメント
Walter Roberson
2022 年 8 月 30 日
No loop needed, no datenum() needed
time = isbetween(joined_eddy.DateTime, joined_eddy.Sunrise, joined_eddy.Sunset, 'closed');
参考
カテゴリ
Help Center および File Exchange で Gravitation, Cosmology & Astrophysics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!