Problems with Datenum / Datestr to convert times and dates to serial time and dates

1 view (last 30 days)
Hello fellow Matlabers,
I have a problem converting times and dates to serial times and dates. I have a matrix containing times and dates, just like this:
'17:05:38' '06/27/12'
'17:05:42' '06/27/12'
...
I need to convert this matrix into one that looks just like this:
'0.712245370' '39625'
'0.712291667' '39625'
....
This is the code I am using so far:
sernums = [datestr( data(2) ), datestr( data(1) )]; output = [sernums data{3} dataC{4}];
... and here is the error I am seeing:
??? Error using ==> datestr at 180 Cannot convert input into specified date string. DATENUM failed..
Does anybody have an idea on what is causing my error? Any help would be greatly appreciated.
Thank you,
Nils

Answers (1)

per isakson
per isakson on 7 Jul 2012
Edited: per isakson on 7 Jul 2012
Try
data = {'17:05:38', '06/27/12'
'17:05:42', '06/27/12' };
out = [ datenum(data(:,1),'HH:MM:SS')-datenum('00:00:00','HH:MM:SS') ...
, datenum(data(:,2),'mm/dd/yy') ];
I assume that you want a numerical output rather than a cell array of strings.
With R2012a
>> datestr( datenum('00:00:00','HH:MM:SS') )
ans =
01-Jan-2012

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!