Subscribing into a timetable using only the time values
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello,
I have created a timetable using the following code:
sz = [120 4]; %Dimensionen of the Table
varTypes = {'string','string' 'string','double'}; %Types of Variables
varNames = {'Radlader','Bagger','Gabelstapler','Lesitung Gesamt'}; %Headings of the table
AL = timetable('Size',sz,'VariableTypes',varTypes,'RowTimes',dt,'VariableNames',varNames); %The table with Aggregatslasten
S = timerange('08:30:00','16:00:00')
AL(S,1:3) = {"Ja"}
I would like the word "Ja" to appear in the first to third column, when the time is between 8:30 and 16:00.
With the code at hand, I get the error message: "A timetable with datetime row times cannot be subscripted using duration values."
How can I solve this?
Note that my timetable is not just for a single day, but a whole week. So I have to keep the dates as well! Specifying the dates does not really work, as the dates are chosen by the user, so they can change, I need a way for the table to only look at the time values.
Thank you for your help!
채택된 답변
Star Strider
2021년 12월 2일
Try this —
sz = [120 4]; %Dimensionen of the Table
varTypes = {'string','string' 'string','double'}; %Types of Variables
varNames = {'Radlader','Bagger','Gabelstapler','Lesitung Gesamt'}; %Headings of the table
dt = datetime('now')+hours(0:sz(1)-1)*4; % Create 'datetime' Array
dt = duration(hour(dt),minute(dt),second(dt));
AL = timetable('Size',sz,'VariableTypes',varTypes,'RowTimes',dt,'VariableNames',varNames); %The table with Aggregatslasten
S = timerange('08:30:00','16:00:00')
S =
timetable timerange subscript:
Select timetable rows with times in the half-open interval:
[08:30:00, 16:00:00)
See Select Timetable Data by Row Time and Variable Type.
AL(S,1:3) = {"Ja"}
AL = 120×4 timetable
Time Radlader Bagger Gabelstapler Lesitung Gesamt
________ _________ _________ ____________ _______________
14:41:31 "Ja" "Ja" "Ja" 0
18:41:31 <missing> <missing> <missing> 0
22:41:31 <missing> <missing> <missing> 0
02:41:31 <missing> <missing> <missing> 0
06:41:31 <missing> <missing> <missing> 0
10:41:31 "Ja" "Ja" "Ja" 0
14:41:31 "Ja" "Ja" "Ja" 0
18:41:31 <missing> <missing> <missing> 0
22:41:31 <missing> <missing> <missing> 0
02:41:31 <missing> <missing> <missing> 0
06:41:31 <missing> <missing> <missing> 0
10:41:31 "Ja" "Ja" "Ja" 0
14:41:31 "Ja" "Ja" "Ja" 0
18:41:31 <missing> <missing> <missing> 0
22:41:31 <missing> <missing> <missing> 0
02:41:31 <missing> <missing> <missing> 0
Experiment to get different results.
.
댓글 수: 5
Thank you, this parshly works!
However in your solution, the dates disappear from the time column. I still need the dates to show up. I tried adding another column by using a datetime I have defined before as "Datum" like so: AL(:,1)= {Datum}
however that somehow doesnt work. It gives me the error:
"Right hand side of an assignment must be a datetime array or text representing dates and times."
I don't get that? Datum is a datetime, so what is that error supposed to mean?
My pleasure.
‘I still need the dates to show up.’
That was not obvious. It also may not be possible with the original approach, since apparently calendarDuration (that will display those times) does not work with the rest of it. The error is that it is not the same as a duration vector in this instance, so a duration array approach will fail.
So, just use regular MATLAB ‘logical indexing’ —
sz = [120 4]; %Dimensionen of the Table
varTypes = {'string','string' 'string','double'}; %Types of Variables
varNames = {'Radlader','Bagger','Gabelstapler','Lesitung Gesamt'}; %Headings of the table
dt = datetime('now')+hours(0:sz(1)-1).'*4; % Create 'datetime' Array
AL = timetable('Size',sz,'VariableTypes',varTypes,'RowTimes',dt,'VariableNames',varNames) %The table with Aggregatslasten
AL = 120×4 timetable
Time Radlader Bagger Gabelstapler Lesitung Gesamt
____________________ _________ _________ ____________ _______________
03-Dec-2021 12:09:14 <missing> <missing> <missing> 0
03-Dec-2021 16:09:14 <missing> <missing> <missing> 0
03-Dec-2021 20:09:14 <missing> <missing> <missing> 0
04-Dec-2021 00:09:14 <missing> <missing> <missing> 0
04-Dec-2021 04:09:14 <missing> <missing> <missing> 0
04-Dec-2021 08:09:14 <missing> <missing> <missing> 0
04-Dec-2021 12:09:14 <missing> <missing> <missing> 0
04-Dec-2021 16:09:14 <missing> <missing> <missing> 0
04-Dec-2021 20:09:14 <missing> <missing> <missing> 0
05-Dec-2021 00:09:14 <missing> <missing> <missing> 0
05-Dec-2021 04:09:14 <missing> <missing> <missing> 0
05-Dec-2021 08:09:14 <missing> <missing> <missing> 0
05-Dec-2021 12:09:14 <missing> <missing> <missing> 0
05-Dec-2021 16:09:14 <missing> <missing> <missing> 0
05-Dec-2021 20:09:14 <missing> <missing> <missing> 0
06-Dec-2021 00:09:14 <missing> <missing> <missing> 0
dtn = rem(datenum(AL.Time),1); % Day Fractions
t1 = rem(datenum('08:30:00', 'HH:mm:ss'),1); % Start Time
t2 = rem(datenum('16:00:00', 'HH:mm:ss'),1); % End Time
S = dtn>=t1 & dtn<t2; % Logical Vector
AL(S,1:3) = {"Ja"}
AL = 120×4 timetable
Time Radlader Bagger Gabelstapler Lesitung Gesamt
____________________ _________ _________ ____________ _______________
03-Dec-2021 12:09:14 "Ja" "Ja" "Ja" 0
03-Dec-2021 16:09:14 <missing> <missing> <missing> 0
03-Dec-2021 20:09:14 <missing> <missing> <missing> 0
04-Dec-2021 00:09:14 <missing> <missing> <missing> 0
04-Dec-2021 04:09:14 <missing> <missing> <missing> 0
04-Dec-2021 08:09:14 "Ja" "Ja" "Ja" 0
04-Dec-2021 12:09:14 "Ja" "Ja" "Ja" 0
04-Dec-2021 16:09:14 <missing> <missing> <missing> 0
04-Dec-2021 20:09:14 <missing> <missing> <missing> 0
05-Dec-2021 00:09:14 <missing> <missing> <missing> 0
05-Dec-2021 04:09:14 <missing> <missing> <missing> 0
05-Dec-2021 08:09:14 "Ja" "Ja" "Ja" 0
05-Dec-2021 12:09:14 "Ja" "Ja" "Ja" 0
05-Dec-2021 16:09:14 <missing> <missing> <missing> 0
05-Dec-2021 20:09:14 <missing> <missing> <missing> 0
06-Dec-2021 00:09:14 <missing> <missing> <missing> 0
That works, and all the date and time information are preserved!
.
Thank you! That worked :)
Can you explain what this does exactly though? I didn't really understand your comments:
dtn = rem(datenum(AL.Time),1);
t1 = rem(datenum('08:30:00', 'HH:mm:ss'),1);
t2 = rem(datenum('16:00:00', 'HH:mm:ss'),1);
My pleasure!
Sure!
The datenum function (that has been available for several decades) produces a decimal fraction depicting days to the left of the decimal separator and fractions of days to the right of the decimal separator. Using the rem or mod functions (similar in most respects, although not the same in all) returns the remainder-after-division. So dividing by 1 returns the decimal part (fractions-of-a-day) as the output. (Experiment with that with any decimal fraction to understand how it works.) This makes it easy to compare times-of-day, and using only the day fraction makes the days themselves irrelevant so that it works across all days.
In detail, then:
dtn = rem(datenum(AL.Time),1); % Day Fractions Of All Days In The 'Time' Variable
t1 = rem(datenum('08:30:00', 'HH:mm:ss'),1); % Day Fraction Equivalent For '08:30:00'
t2 = rem(datenum('16:00:00', 'HH:mm:ss'),1); % Day Fraction Equivalent For '16:00:00'
Then the ‘S’ assignment uses these to create a logical vector to produce the desired end result.
.
And,
If my Answer helped you solve your problem, please Accept it!
.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Time Series Events에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
