How do I Subscript Duration Row times with Date Time values?
Afficher commentaires plus anciens
Hello,
I am plotting some data from a .csv file, one year at a time. When I select a certain time range, I am given the error, "A timetable with duration row times cannot be subscripted using datetime values."
I am attaching the code below, and it says the error is in the sixth line (TT2 = TT(TR,6);). Any help is appreciated, thanks!
[
T = readtable('QuakeTrials.csv');
TT = table2timetable(T);
TT(3:7,:)
TR = timerange('2017-01-01','2017-12-31');
TT2 = TT(TR,6);
TT2(3:7,:)
1 commentaire
Paolo
le 19 Juin 2018
Can you please attach the .csv file?
Réponses (5)
Xel Ch
le 19 Juin 2018
0 votes
4 commentaires
Paolo
le 19 Juin 2018
In your .csv file you have:
Year Date
2017 1 2 23:18:23 2017:01:02
2017 1 3 04:17:25 2017:01:03
2017 1 6 05:03:17 2017:01:06
2018 1 7 16:51:36 2018:01:07
2018 1 17 11:11:34 2018:01:17
Am I right in assuming that the correct format for the dates should be something like 2017/1/2 23:18:23 (first row) ?
Xel Ch
le 19 Juin 2018
Xel Ch
le 19 Juin 2018
Paolo
le 19 Juin 2018
Right, I am assuming that '2017:01:01' is 'yyyy:mm:dd'. I have submitted an answer for the updated QuakeTrials.csv below, hope that it solves the problem.
Walter Roberson
le 19 Juin 2018
Modifié(e) : Walter Roberson
le 19 Juin 2018
T = readtable('QuakeTrials.csv');
T = T(3:end,:);
dt = datetime(T{:,1}, T{:,2}, T{:,3}) + T{:,4};
TT = table2timetable(T, 'rowtimes', dt);
TR = timerange('2017-01-01','2017-12-31');
TT_lat = TT(TR,6)
1 commentaire
Walter Roberson
le 19 Juin 2018
In R2018a the above code produces
TT_lat =
3×1 timetable
Time Latitude
____________________ ________
02-Jan-2017 23:18:23 46.59
03-Jan-2017 04:17:25 45.81
06-Jan-2017 05:03:17 45.13
In earlier versions you might need to do
filename = 'QuakeTrials.csv';
opt = detectImportOptions(filename);
opt = setvartype(opt, 4, 'datetime');
T = readtable(filename, opt);
%do not subselect T(3:end,:) here as detectImportOptions already skips it
dt = datetime(T{:,1}, T{:,2}, T{:,3}) + (T{:,4} - dateshift(T{:,4},'start','day'));
TT = table2timetable(T, 'rowtimes', dt);
TR = timerange('2017-01-01','2017-12-31');
TT_lat = TT(TR,6)
Tested in R2017b.
opts = detectImportOptions('QuakeTrials.csv');
opts.MissingRule = 'omitvar';
T = readtable('QuakeTrials.csv',opts);
T.Date = cellfun(@(x) datetime(x,'InputFormat','yyyy:mm:dd','Format','dd/mm/yyyy'),T.Date);
TT = table2timetable(T);
TR = timerange('01/02/2017','30/12/2017');
TT2 = TT(TR,6);
TT2 =
3×1 timetable
Date Var13
__________ _____
02/01/2017 1.6
03/01/2017 2.1
06/01/2017 1.4
Xel Ch
le 19 Juin 2018
8 commentaires
Works fine on Matlab2017b... try changing that line to:
T.Date = datetime(T.Date,'InputFormat','yyyy:mm:dd','Format','dd/mm/yyyy')
P.s. please submit comments and not answers when replying to someone.
Xel Ch
le 19 Juin 2018
Paolo
le 19 Juin 2018
No worries at all. Right, from your previous comment the input is actually of type duration, so please ignore my previous comment.
Could you put a breakpoint at the line T.Date ... and show what T.Date is? Perhaps the behavior of readtable has changed in 2018a and it no longer reads T.Date as a cell array but as a duration array.
Walter Roberson
le 19 Juin 2018
I showed how to handle that in my response.
dt = datetime(T{:,1}, T{:,2}, T{:,3}) + T{:,4};
Xel Ch
le 19 Juin 2018
Walter Roberson
le 19 Juin 2018
duration is a datetype that is a relative amount of clock time. You can process the durations the way I show -- T{:,4} is the duration column, and you can add it to a datetime created from the year, month, day from the first three columns.
Paolo
le 19 Juin 2018
@Walter The behavior for readtable must have changed then because I am unable to run your solution. T{:,2} and T{:,3} are in fact NaN.
Error in game (line 266)
dt = datetime(T{:,1}, T{:,2}, T{:,3}) + T{:,4};
Parameter name must be text.
@Alexander You can place a breakpoint by clicking on the gray vertical line of the editor, a red circle will appear. Yes, T.Date is definitely of type duration. Have you tried using the solution proposed by Walter?
Alternatively you can try:
T.Date = char(duration(T.Date,'Format','hh:mm:ss'))
T.Date = datetime(T.Date,'InputFormat','yyyy:mm:dd','Format','dd/mm/yyyy')
Walter Roberson
le 19 Juin 2018
Looks like R2018a added duration support. I have updated my answer.
Peter Perkins
le 5 Juil 2018
I'm coming to this thread late, so I may be repeating what Walter and Paolo already sorted out. It looks like the root cause was that in R2018a, readtable began supporting directly reading durations as well as datetimes. And because the "time" stamps precede the "date" stamps in the file, this line from the table2timetable doc
"The first datetime or duration variable in T becomes TT's time vector"
explains what happened. Walter's suggestion seems right, although I would have used
dt = T.Date + T.Var4; % csv is missing some col headings
By the way, it's easy to switch a timetable back and forth between absolute and relative time: just add or subtract an offset:
>> tt = timetable([1;2;3],'RowTimes',datetime('now') + minutes(0:2))
tt =
3×1 timetable
Time Var1
____________________ ____
05-Jul-2018 10:33:50 1
05-Jul-2018 10:34:50 2
05-Jul-2018 10:35:50 3
>> t1 = tt.Time(1)
t1 =
datetime
05-Jul-2018 10:33:50
>> tt.Time = tt.Time - t1
tt =
3×1 timetable
Time Var1
________ ____
00:00:00 1
00:01:00 2
00:02:00 3
>> tt.Time = tt.Time + t1
tt =
3×1 timetable
Time Var1
____________________ ____
05-Jul-2018 10:33:50 1
05-Jul-2018 10:34:50 2
05-Jul-2018 10:35:50 3
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!