仅基于时间(而非日期)在Matlab中定义时间范围

仅基于时间(而非日期)在Matlab中定义时间范围,matlab,timetable,Matlab,Timetable,我尝试使用Matlab timerange函数选择特定时间间隔内的时间表行,例如,在12:00:00和16:00:00之间。因为我有30个不同的日子,每天都有不同的时间安排,所以我希望忽略日期并检索所有时间在我的时间间隔内的行,而不管是哪一天。如果我只在下面的行中写时间,Matlab默认使用今天。如果有人能帮我找出如何只使用时间(而不是日期)作为索引,我将不胜感激 S = timerange('??? 12:00:00','??? 16:00:00'); Output = TT(S,:); 下

我尝试使用Matlab timerange函数选择特定时间间隔内的时间表行,例如,在12:00:00和16:00:00之间。因为我有30个不同的日子,每天都有不同的时间安排,所以我希望忽略日期并检索所有时间在我的时间间隔内的行,而不管是哪一天。如果我只在下面的行中写时间,Matlab默认使用今天。如果有人能帮我找出如何只使用时间(而不是日期)作为索引,我将不胜感激

S = timerange('??? 12:00:00','??? 16:00:00');
Output = TT(S,:);

下面是一种在显式删除除时间信息以外的所有内容后选择行的方法。将
TT
作为输入时间表

% Create another timetable with the same time information
%  It also has one data column which represents the row index
tempTT = timetable(TT.Time, (1:height(TT))');

% remove the Year, Month, and Date information
tempTT.Time.Year  = 1;
tempTT.Time.Month = 1;
tempTT.Time.Day   = 1;

% Select the desired timerange
S = timerange('1-1-1 12:00:00','1-1-1 16:00:00');

% Get the row indices of the selected rows
idxSel = tempTT(S,:).Variables;

% Select the desired rows from the original timetable
Output = TT(idxSel,:);

下面是一种在显式删除除时间信息以外的所有内容后选择行的方法。将
TT
作为输入时间表

% Create another timetable with the same time information
%  It also has one data column which represents the row index
tempTT = timetable(TT.Time, (1:height(TT))');

% remove the Year, Month, and Date information
tempTT.Time.Year  = 1;
tempTT.Time.Month = 1;
tempTT.Time.Day   = 1;

% Select the desired timerange
S = timerange('1-1-1 12:00:00','1-1-1 16:00:00');

% Get the row indices of the selected rows
idxSel = tempTT(S,:).Variables;

% Select the desired rows from the original timetable
Output = TT(idxSel,:);

除了@aksadv的答案之外,还有一个稍微简洁的解决方案,使用
duration
类型,如下所示:

times = datetime(2017, 01, randi([1 31], 10, 1), ...
    randi(24, 10, 1), randi(60, 10, 1), randi(60, 10, 1));
tt = timetable(times, rand(10, 1));
% Use TIMEOFDAY to convert datetimes into durations representing
% the time of day:
tt.times = timeofday(tt.times)
% TIMERANGE can be used with durations too. Use HOURS to construct
% the duration objects
tt(timerange(hours(12), hours(16)), :)

这用于提取时间成分,并创建
duration
实例。

除了@aksadv的答案之外,还有一种使用
duration
类型的更简洁的解决方案,如下所示:

times = datetime(2017, 01, randi([1 31], 10, 1), ...
    randi(24, 10, 1), randi(60, 10, 1), randi(60, 10, 1));
tt = timetable(times, rand(10, 1));
% Use TIMEOFDAY to convert datetimes into durations representing
% the time of day:
tt.times = timeofday(tt.times)
% TIMERANGE can be used with durations too. Use HOURS to construct
% the duration objects
tt(timerange(hours(12), hours(16)), :)
这将用于提取时间组件,并创建
持续时间
实例