使用datenum()检查给定日期时间是否在预定义的时间间隔内 [英] Checking if a given date hour is within a predefined interval with datenum()

查看:203
本文介绍了使用datenum()检查给定日期时间是否在预定义的时间间隔内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有日期(和其他东西)的表格,我从CSV文件中提取出来。为了对我的数据进行一些处理(包括绘图),我决定将所有的日期字符串转换为日期数字(为简单起见,下面我将排除所有剩余的数据,仅集中在日期,因此不要记住从日期到时间表的步骤,以及可以省略的事实):

I have a table with dates (and other things), which I have extracted from a CSV file. In order to do some processing of my data (including plotting) I decided to convert all my date-strings to date-numbers (below for simplicity reasons I will exclude all the rest of the data and concentrate on the dates only so don't mind the step from dates to timetable and the fact that it can be omitted):

dates = [7.330249777777778e+05;7.330249291666667e+05;7.330246729166667;7.330245256944444;7.330246763888889;7.330245284722222;7.330245326388889;7.330246625000000];

timetable = table(dates);

timetable
_________
7.330249777777778e+05
7.330249291666667e+05
7.330246729166667
7.330245256944444
7.330246763888889
7.330245284722222
7.330245326388889
7.330246625000000

我正面临以下问题 - 基于白天我想告诉用户一个日期是否在早上(24小时比例:5-12小时),中午(12-13小时),下午(13-18小时),晚上(18-21小时),晚上(21-5h)根据我存储在我的表中的日期。如果我有一个日期向量(元素:年,月,日,小时,分钟,秒),这将是非常简单的:

I'm facing the following issue - based on the time during the day I want to tell the user if a date is in the morning (24-hours scale: 5-12h), noon (12-13h), afternoon (13-18h), evening (18-21h), night (21-5h) based on the date I have stored in my table. In case I had a date-vector (with elements: year,month,day,hour,minute,second) it would be pretty straight forward:

for date = 1:size(timetable)
    switch timetable(date).hour
      case {5,12}
        'morning'
      case {12,13}
        'noon'
      case {13,18}
        'afternoon'
      case {18,21}
        'evening'
      otherwise
        'night'
    end
end

With 7.330246729166667 ,其余这一点对我而言并不明显。任何想法如何避免转换为其他日期格式只是为了这一步,同时避免一些复杂的公式提取所需的数据(不一定只是小时,但我对其余的兴趣)?

With 7.330246729166667 and the rest this is not that obvious at least to me. Any idea how to avoid converting to some other date-format just for this step and at the same time avoid some complex formula for extracting the required data (not necessarily hour only but I'm interested in the rest too)?

推荐答案

Matlab序列号中的一个单位相当于1天,即24小时。知道这一点,您可以将您定义的日内桶内的日期的小数部分(请注意,您的开关仅适用于完全等于案例列表):

One unit in Matlab serial dates is equivalent to 1 day, i.e. 24 hours. Knowing this, you can bin the fractional part of the the dates within the intraday buckets you defined (note that your switch will only work for values exactly equal to the case lists):

bins  = {'morning', 'noon', 'afternoon', 'evening', 'night'};
edges = [5,12,13,18,21,25]./24; % As fraction of a day

% Take fractional part
time = mod(dates,1);

% Bin with lb <= x < ub, where e.g. lb = 5/25 and is ub = 12/24
[counts,~,pos] = histcounts(time, edges);
% Make sure unbinned x in [0,5) are assigned 'night' 
pos(pos==0) = 5;

bins(pos)'
ans = 
    'night'
    'night'
    'morning'
    'morning'
    'morning'
    'morning'
    'morning'
    'morning'

这篇关于使用datenum()检查给定日期时间是否在预定义的时间间隔内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆