a question regarding the format of dates

Dear all, I have an excel file that contains among other columns, a date column. In that column the dates are either in this format '01/02/2009' or in that format ' 03/2009'
For instance
Out={
'Con' dates'
SP '01/02/2009'
kp ' 03/2009'}
The problem is that when I import this matrix in Stata then I get something like
Out={
'Con' dates'
SP 39722
kp ' 03/2009'}
So all the dates represented in this format '01/02/2009' appear as numbers in stata. Can I fix that within Matlab or within stata?

3 Kommentare

antonet
antonet am 31 Jan. 2013
Bearbeitet: antonet am 31 Jan. 2013
I suspect that I will have to change all the dates that appear as '01/02/2009' to '02/2009' . IS there any code for doing that?
Thanks
the cyclist
the cyclist am 31 Jan. 2013
What does it look like if you import directly into MATLAB, rather than Stata?
The space at the beginning of ' 03/2009' suggests to me that field is being interpreted as text, not as a date. You may be able to fiddle with the format within Excel to get them all the same.
antonet
antonet am 1 Feb. 2013
Bearbeitet: antonet am 1 Feb. 2013
In matlab looks like ok when I import this matrix, that is
Out={
'Con' dates'
SP '01/02/2009'
kp ' 03/2009'}

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Walter Roberson
Walter Roberson am 1 Feb. 2013

0 Stimmen

needfix = strcmpn(Out, ' ', 1);
Out(needfix) = cellfun( @(S) ['01/' S(2:end)], Out(needfix), 'Uniform', 0);
And then convert the dates.

3 Kommentare

Hi,
I applied this code
needfix = strcmpn(Out, ' ', 1);
Out(needfix) = cellfun( @(S) ['01/' S(2:end)], Out(needfix), 'Uniform', 0);
to
Out={
'Con' 'dates'
'SP' '01/02/2009'
'kp' ' 03/2009'}
but I get this error message
??? Undefined function or method 'strcmpn' for input arguments of type 'cell'.
Is there any way to fix that?
thank
antonet
antonet am 1 Feb. 2013
is there a solution?
thank you
antonet
antonet am 1 Feb. 2013
Bearbeitet: antonet am 1 Feb. 2013
Could you please give me some help? Am I doing anything wrong?
thank you

Melden Sie sich an, um zu kommentieren.

owr
owr am 1 Feb. 2013

0 Stimmen

If I were in your situation, I would try and do the formatting in Excel first and get all dates uniform before importing into MATLAB (or Stata). Excel in my experience seems to be better at "figuring out what you meant".
Try just highlighting and formatting the column in Excel into any fairly normal date format. It will change something like "3/2009" to "3/1/2009" for you. Once data is uniform, import into MATLAB. Even if you have to do this for a number of files it may be quicker in the long run than writing a general date parser in ML.

Kategorien

Tags

Gefragt:

am 31 Jan. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by