How do I combine two DateNumber strings without resulting in all NaN values?

2 views (last 30 days)
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
Ben Flett
Ben Flett on 9 Aug 2017
Okay thanks Jan. The real problem lies with the Date format, as I said i'm unsure of any way to import the date which has a format of both yyyy-MM-dd HH:mm:ss and yyyy-MM-dd HH:mm:ss.SSSSSS. Hence trying to find a way around this.
Would you suggest anyway of importing the data so that both formats could be read? Sorry for any foolish questions; i'm all very new to this.
Jan
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.

Sign in to comment.

Answers (3)

Jan
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);

Peter Perkins
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

Jan
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')

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!