Date format changes at midnight
Ältere Kommentare anzeigen
Hey, I am reading an excel log file. In the first column (as I see it in Excel) the date format is 'dd/MM/YYYY HH:mm:ss'
example: '27/12/2017 22:15:00'
When I read the xls:
[A,rawData,All] = xlsread(FileName,'Sheet1');
I get the the same format:
rawData(1,1) = {'27/12/2017 22:15:00'}
EXCEPT when the hour is 00:00:00, then I get
rawData(2,1) = {'27/12/2017'}
Which is a problem later on, when I try to divide the data into 15 min, hourly, daily and weekly buckets when I use
datevec, datetime, datenum
I could, in theory, perform a loop to look for such instances and add the 00:00:00 manually, but my data relativaly large (around 300,000 samples).
Any suggestions?
Akzeptierte Antwort
Weitere Antworten (1)
dpb
am 3 Feb. 2018
It's unfortunate the ML time functions aren't more forgiving of such cases.
Best I can think of otomh is to either do the fixup on the input array by testing for length and adding the trailing string or do the processing in a loop with a try...catch...end block; you put the format string with the time string in the try section and the one without in the catch.
Oh, another way that's a little cleaner code-wise; don't know how it would compare in run time...
>> dn=zeros(size(rawData)); % preallocate output datenum vector
>> ixtim=(cellfun(@length,rawData))>10; % logical addressing vector elements with time
>> dn(ixtim)=datenum(rawData(ixtim),'dd/mm/yyyy HH:MM:SS'); % ones with time string
>> dn(~ixtim)=datenum(rawData(~ixtim),'dd/mm/yyyy'); % those without
>> datestr(dn) % what we got...
ans =
27-Dec-2017 22:15:00
27-Dec-2017 00:00:00
>>
2 Kommentare
Walter Roberson
am 3 Feb. 2018
rawData(:,1) = cellstr(datestr(cellfun(@datenum, rawData(:,1)),'dd/mm/yyyy HH:MM:SS'));
dpb
am 3 Feb. 2018
Clever way to do the text fixup, Walter. Didn't take the time to compare for large array sizes the cost of the datenum, datestr pair compared to the lookup; there are two datenum calls there as well so probably nothing significant difference-wise.
Kategorien
Mehr zu Time Series Objects finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!