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

2 次查看(过去 30 天)
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

回答(1 个)

per isakson
per isakson 2012-7-7
编辑:per isakson 2012-7-7
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

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by