Divide daytimes in before noon and after noon

2 ビュー (過去 30 日間)
Philipp Henschel
Philipp Henschel 2018 年 1 月 8 日
コメント済み: Philipp Henschel 2018 年 1 月 15 日
I'm working on a table with several variables. One of them is time of measurement in a 'yy-mm-dd hh:mm:ss' format. The measurements go over several days and I want to divide them into to groups: before noon and after noon. Right now I'm converting the dates to a string and then stringcompare them. Is there a better way? Since I think this way is unpleasent.
Thank you I advance!

回答 (1 件)

Matt Sprague
Matt Sprague 2018 年 1 月 11 日
You could use the Hour property of the datetime class and logical indexing to extract which times are before or noon.
t1 = datetime(18,01,1,0,0,0,'Format','yy-MM-dd hh:mm:ss');
t2 = datetime(18,01,1,23,59,59,'Format','yy-MM-dd hh:mm:ss');
t = t1:hours:t2;
%
morning = t(t.Hour<12);
afternoon = t(t.Hour>=12);
  1 件のコメント
Philipp Henschel
Philipp Henschel 2018 年 1 月 15 日
Thanks for your Answer! By now I found an different solution using the timerange feature and converting the table to a timetable
%before noon = bn, after noon = an ;
t_bn = timerange ('2013-02-15 11:00:00', '2013-02-15 16:59:59');
t_an = timerange ('2013-02-15 17:00:00', '2013-02-15 23:59:59');
fr_eb_bn = fr_eb_daytimes(t_bn,:);
fr_eb_an = fr_eb_daytimes(t_an,:);
But I'm still not happy with the solution because now separation is only for one day possible an I'm aiming to divide more days into before noon and after noon. Any further ideas?
P.S.: timeranging for every single day is not helpful

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by