Divide daytimes in before noon and after noon

4 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by