How do I combine two DateNumber strings without resulting in all NaN values?
2 views (last 30 days)
Show older comments
I have two different DateNumber strings DateNumber1 and DateNumber2. I created these from the same dataset as the Date formatting changes from yyyy-MM-dd HH:mm:ss to yyyy-MM-dd HH:mm:ss.SSSSSS resulting in large amounts of NaN values when I import the data into MATLAB. I've imported the two date formats separately and used datenum to create two different Matrixes. I now want to try and combine them, infilling the NaN values with the missing data, however addition will not work and neither will combining.
Any advice would be hugely appreciated!
3 Comments
Jan
on 9 Aug 2017
Please show us, how the input data are looking. Is this a text file or a MAT file containing a datetime object? Please post the relevant part of your code.
Answers (3)
Jan
on 9 Aug 2017
After datenum you have two double vectors. Then all details concerning dates are not relevant for question.
d1 = [1, NaN, 2, 3, 4, NaN, 5];
d2 = [NaN, 6, NaN, NaN, NaN, 7, NaN];
d12 = d1;
idx = isnan(d1);
d12(idx) = d2(idx);
0 Comments
Peter Perkins
on 10 Aug 2017
You say datenum, but you are using a format for datetimes. And in any case, datenum only accepts three fractional seconds digits, you have six.
It sounds like you have text data with two different formats. If you are using a version of MATLAB since R2014b, I'd suggest using datetime, not dateum.
>> s = {'2017-08-10 09:10:11'; '2017-08-10 09:10:12'; '2017-08-10 09:10:13'; '2017-08-10 09:10:14.123456'; '2017-08-10 09:10:15.234567'}
s =
5×1 cell array
{'2017-08-10 09:10:11' }
{'2017-08-10 09:10:12' }
{'2017-08-10 09:10:13' }
{'2017-08-10 09:10:14.123456'}
{'2017-08-10 09:10:15.234567'}
>> d = datetime(s,'InputFormat','yyyy-MM-dd HH:mm:ss','Format','yyyy-MM-dd HH:mm:ss.SSSSSS')
d =
5×1 datetime array
2017-08-10 09:10:11.000000
2017-08-10 09:10:12.000000
2017-08-10 09:10:13.000000
NaT
NaT
>> d(isnat(d)) = datetime(s(isnat(d)),'InputFormat','yyyy-MM-dd HH:mm:ss.SSSSSS')
d =
5×1 datetime array
2017-08-10 09:10:11.000000
2017-08-10 09:10:12.000000
2017-08-10 09:10:13.000000
2017-08-10 09:10:14.123456
2017-08-10 09:10:15.234567
0 Comments
Jan
on 10 Aug 2017
What about:
s = {'2017-08-10 09:10:11'; ...
'2017-08-10 09:10:12'; ...
'2017-08-10 09:10:13'; ...
'2017-08-10 09:10:14.123456'; ...
'2017-08-10 09:10:15.234567'};
short = cellfun('length', s) < 26;
s(short) = strcat(s(short), '.000000');
d = datetime(s, 'InputFormat', 'yyyy-MM-dd HH:mm:ss.SSSSSS', ...
'Format', 'yyyy-MM-dd HH:mm:ss')
0 Comments
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!