Return the last time in a datetime column containing NaT
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Numbers
am 19 Mär. 2024
Kommentiert: Stephen23
am 19 Mär. 2024
Hi, I'm trying to get the value of the last recognised time entry in an imported excel column of data. At a point, the column changes from datetime values to NaT values, I think due to some of the other columns being longer than my datetime column. It's not practical to edit the excel file to use a simple function. I have tried using find with ~isnat,but I'm not sure I'm applying it correctly. See code below and sample data attached. Any help is appreciated!
ReadSS = readtable('2columns.xlsx','Sheet','Trend Data')
TimeCol = datetime(ReadSS{:,1}, 'InputFormat', 'dd/MM/yyyy hh:mm:ss.SSSSSS a ''', 'TimeZone', 'UTC'); %identify the format of time and date in excel column 1
TimeCol.Format = 'dd/MM/uuuu HH:mm:ss.SSSSSS'; %Set the format of the time data in matlab
first_ele=TimeCol(1,:) % first value
last_ele=TimeCol(end,:) %last value
last_ele= find(~isnat(TimeCol(end,:)))
0 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 19 Mär. 2024
Your data is already a column, using 1 and end as indices on it will provide scalars (see the edit above).
ReadSS = readtable('2columns.xlsx','Sheet','Trend Data')
%You can include the output format in datetime() call
TimeCol = datetime(ReadSS{:,1}, 'InputFormat', 'dd/MM/yyyy hh:mm:ss.SSSSSS a ''', 'TimeZone', 'UTC', ...
'Format', 'dd/MM/uuuu HH:mm:ss.SSSSSS'); %identify the format of time and date in excel column 1
%Set the format of the time data in matlab
Specify the direction of search i.e. last in the find() call -
%Find the last not-a-Time value in the given column data
last_ele= find(~isnat(TimeCol), 1, 'last')
6 Kommentare
Voss
am 19 Mär. 2024
I see what you're saying about the 12000 datetimes. Nevertheless, NaT is also a datetime, and in a table all columns must be the same length, so technically there are 28981 datetimes in column 1.
You should accept Dyuman Joshi's answer, as my response was merely a follow-up comment.
Stephen23
am 19 Mär. 2024
"I think it's 12000 datetime... with the rest of the column importing as NaT"
As Voss correctly wrote, NaT are also DATETIME objects. This is very easy to confirm:
isdatetime(NaT)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Timetables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!