Divide daytimes in before noon and after noon
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
0 commentaires
Réponses (1)
Matt Sprague
le 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 commentaire
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!