2017-01-19 7 views
0

Matlab timerange 함수를 사용하여 특정 시간 간격 내에 시간표 행을 선택하려고합니다. 12:00:00에서 16:00:00 사이 매일 30 일씩 다양한 타이밍을 다루기 때문에 매일을 무시하고 내 시간 간격에 속하는 모든 행을 검색하고 싶습니다. 아래 줄에 시간 만 쓰면 Matlab은 기본적으로 오늘을 사용합니다. 누군가가 시간 (그리고 날짜가 아닌)만을 색인으로 사용하는 방법을 찾는 것을 도울 수 있다면 매우 감사 할 것입니다.matlab에 timerange를 단순히 시간 (날짜 아님)을 기반으로 정의하십시오.

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

답변

1

, 그래서 같은 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)), :) 

이 시간 구성 요소를 추출하고, hoursduration 인스턴스를 생성 timeofday를 사용합니다.

1

여기에는 시간 정보를 제외한 모든 항목을 명시 적으로 제거한 후 행을 선택하는 방법이 나와 있습니다. TT을 입력 시간표로 설정하십시오. @ aksadv의 대답 또한

% 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,:);