Date Conversion

Hi,
I have a <200x1 cell> array which consists of dates read through a text file. The dates are in the format yyyy/mm/dd hh:mm:ss:fff. I want to convert these dates into a matlab recognizable date vector which could then be later used in creating time series objects. I am not sure how to do this.
Thanks,

2 Commenti

Jan
Jan il 19 Dic 2011
Are you sure about the colon before the fractional seconds? Usually there is a dot.
Syed Abbas
Syed Abbas il 19 Dic 2011
Oh, that works now! I mistook the colun for the dot. Thansk a lot for ponting that out.

Accedi per commentare.

 Risposta accettata

Fangjun Jiang
Fangjun Jiang il 19 Dic 2011

0 voti

datenum()
>> dates={'2011/11/11 11:11:11:111';'2012/12/12 12:12:12:121'}
datenum(dates,'yyyy/mm/dd HH:MM:SS:FFF')
dates =
'2011/11/11 11:11:11:111'
'2012/12/12 12:12:12:121'
ans =
1.0e+005 *
7.3482
7.3522

9 Commenti

Syed Abbas
Syed Abbas il 19 Dic 2011
I get the following error when I use datenum():
Error using datenum (line 181)
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed on converting date string to date number.
datenum() works if I have a single date but I have a date variable of type [200 x 1 cell] and want to convert all the dates in this variable at once
Jan
Jan il 19 Dic 2011
Please post the inputs you are using.
Syed Abbas
Syed Abbas il 19 Dic 2011
I keep getting the same error I meantioned earlier
Jan
Jan il 19 Dic 2011
For which input? Please post an exact copy.
Fangjun Jiang
Fangjun Jiang il 19 Dic 2011
Copy the exact command in the answer. You see it worked fine on my computer.
Syed Abbas
Syed Abbas il 19 Dic 2011
Thats how my imputs look like:
'2011/12/15 09:00:00.039'
'2011/12/15 09:00:00.039'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.321'
'2011/12/15 09:00:00.323'
'2011/12/15 09:00:00.333'
'2011/12/15 09:00:00.333'
'2011/12/15 09:00:00.333'
'2011/12/15 09:00:00.333'
'2011/12/15 09:00:00.333'
'2011/12/15 09:00:00.333'
'2011/12/15 09:00:00.333'
'2011/12/15 09:00:00.333'
'2011/12/15 09:00:00.333'
'2011/12/15 09:00:00.353'
'2011/12/15 09:00:00.353'
'2011/12/15 09:00:00.355'
'2011/12/15 09:00:00.359'
'2011/12/15 09:00:00.373'
'2011/12/15 09:00:00.402'
'2011/12/15 09:00:00.407'
'2011/12/15 09:00:00.437'
'2011/12/15 09:00:00.437'
'2011/12/15 09:00:00.437'
Fangjun Jiang
Fangjun Jiang il 19 Dic 2011
And the variable dates is a 2x1 cell array. It should be representative to your 200x1 cell array.
Fangjun Jiang
Fangjun Jiang il 19 Dic 2011
Change the last column symbol ":" in the format string to ".". Your data is different than you described in your question. Jan was right. It usually is HH:MM:SS.FFF, not HH:MM:SS:FFF
Fangjun Jiang
Fangjun Jiang il 19 Dic 2011
Or you don't need to specify the format anymore since it is standard.
%%
Dates={'2011/12/15 09:00:00.039';'2011/12/15 09:00:00.039'};
datenum(Dates)

Accedi per commentare.

Più risposte (3)

Walter Roberson
Walter Roberson il 19 Dic 2011

0 voti

4 Commenti

Syed Abbas
Syed Abbas il 19 Dic 2011
I get the following error when I use datenum():
Error using datenum (line 181)
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed on converting date string to date number.
datenum() works if I have a single date but I have a date variable of type [200 x 1 cell] and want to convert all the dates in this variable at once
Walter Roberson
Walter Roberson il 19 Dic 2011
What does size() and class() show for the first element of your cell array, YourCell{1} ?
Are the strings absolutely consistent?
What happens if you try
cellfun(@(S) datenum(S, 'yyyy/mm/dd HH:MM:SS.FFF'), YourCell)
At the moment, it seems to me that either you do not have a cell array of strings, or else that at least one of the entries is not in the expected format.
Syed Abbas
Syed Abbas il 19 Dic 2011
size shows 1 23 and class shows char
Syed Abbas
Syed Abbas il 19 Dic 2011
I get the following error with cell fun:
Error using cellfun
Input #2 expected to be a cell array, was char instead.

Accedi per commentare.

Jose Jeremias Caballero
Jose Jeremias Caballero il 19 Dic 2011

0 voti

Hello.
>> A={'2011/12/19 13:27:50.890';'2012/12/19 18:49:40.790'}
A =
'2011/12/19 13:27:50.890'
'2012/12/19 18:49:40.790'
>> vector=datevec(A, 'yyyy/mm/dd HH:MM:SS.FFF')
vector =
1.0e+003 *
2.0110 0.0120 0.0190 0.0130 0.0270 0.0509
2.0120 0.0120 0.0190 0.0180 0.0490 0.0408
Jan
Jan il 19 Dic 2011

0 voti

A less intelligent, but much faster method than DATENUM:
dates = {'2011/11/11 11:11:11.111'; ...
'2012/12/12 12:12:12.121'};
function Vector = myDateConversion(DateCell)
S = sprintf('%s ', DateCell{:});
D = sscanf(S, '%d/%d/%d %d:%d:%f');
Vector = transpose(reshape(D, 6, []));
On Matlab 2009a this is 5 times faster than datevec(dates, 'yyyy/mm/dd HH:MM:SS.FFF'), but datevec is much smarter and converts the '2011/32/12' correctly.
The date topic has been discussed in Mike's blog recently:

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by