Divide daytimes in before noon and after noon

2 views (last 30 days)
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!

Answers (1)

Matt Sprague
Matt Sprague on 11 Jan 2018
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 Comment
Philipp Henschel
Philipp Henschel on 15 Jan 2018
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

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!