Subscribing into a timetable using only the time values

3 次查看(过去 30 天)
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
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 个评论
Star Strider
Star Strider 2021-12-4
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.
.
Star Strider
Star Strider 2021-12-4
And,
If my Answer helped you solve your problem, please Accept it!
.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Timetables 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by