MATLAB: Combining Date and Time variables (class – datetime) into 1 DateTime (class – double) array

datenumdatetime

I import my data with:
Date = dataArray{:, 6}; Time = dataArray{:, 7};
Date as a 'datetime' value with format: 'dd.mm.yyyy' and
Time as a 'datetime' value with format: 'HH:MM'.
I can convert both of these using datenum:
Date2=datenum(Date); Time2=datenum(Time);
How do I now combine Date2 and Time2 to make one DateTime array?
Example: Date= 06.07.2010
06.07.2010
Time= 22:31
22:41
Date2 = 7.341440048611110e+05
7.341440048611110e+05
Time2 = 7.359039381944444e+05
7.359039451388889e+05
I tried including a format specification in the datenum code line but kept on getting an error (Erro using Datetime/datenum – Too many input arguments)
Date2 = datenum(Date, 'dd.mm.yyyy');
Time2 = datenum(Time, 'HH:MM');
I would like to generate one DateTime array that would look as follows as a string:
06.07.2010 22:31
06.07.2010 22:41
Thanks in advance, Jenny

Best Answer

I haven’t used the new datetime functions much, and I don’t have your array. This works on the example you gave:
Date = datetime('06.07.2010','InputFormat','dd.MM.yyyy');
Time = datetime('22:31','InputFormat','HH:mm');
CnvtDT = @(Date,Time) datetime([Date.Year Date.Month Date.Day Time.Hour Time.Minute 0], 'Format', 'dd.MM.yyyy HH:mm');
DateTime2 = CnvtDT(Date,Time) % Check Output
The output is also a ‘datetime’ structure.