MATLAB: Wrong Conversion problem from datenum

data acquisitionMATLAB

In a Serial data receiving process, I have a incoming data
press = {'2016-04-14' '14:18:48' '321'};
datenum(datestr(cellfun(@(x,y) [x y],press(1),press(2),'un',0)));
Here, the result of date time serial from above step,
ans = 7.3648e+05
When I convert back, the result is giving a different time into the future:
datestr(ans)
ans = 29-May-2016 22:18:48
where this is working wrong?

Best Answer

Look at the char array you're creating with your cellfun call.
>> q = cellfun(@(x,y) [x y],press(1),press(2),'un',0)
q =
'2016-04-1414:18:48'
It looks like somehow datestr is interpreting your string with format 0 (as described in the help text) and treating the 1414 part as a number of days somehow. You can correct this by adding in the missing space:
>> q2 = sprintf('%s ', press{1:2})
I would also recommend that if you're using a release that contains the datetime function that you avoid using serial date numbers and create a datetime instead.