Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am having a hard time trying to import some text data for later use in DateListPlot. My first field in my data is a date of the format "yyyymmdd". I tried to use both ReadList and Import, but I cannot get DateListPlot to understand that my date is truly a date.

data = 
 Import["/path/to/data.txt", "Table", "DateStringFormat" -> {"Year", "Month", "Day"}] //Short
{{20131217, 123123.}, <<50>>, {20140313, 321321.}}
DateListPlot[data, Filling -> Axis]

DateListPlot::ntdt: The first argument to DateListPlot should be a list of pairs of dates and real values, a list of real values, or a list of several such lists. >>

With ReadList I tried to see if I could use StringSplit, but that didn't seem to work either.

Any pointers would be most appreciated.

Edit: I added some more data.

data = Import[
"/path/to/file.txt", 
"Table", "DateStringFormat" -> {"Year", "Month", "Day"}] //TableForm

{
 {20131217, 114812.},
 {20131220, 114192.},
 {20131221, 114192.},
 {20131223, 114192.},
 {20131224, 114504.},
 {20131226, 115408.},
 {20131227, 115165.}
}
share|improve this question
1  
You may want to post a few lines of your txt file –  belisarius Mar 14 at 2:09
    
I can not reproduce your problem. With the two data points you have provided, I get perfectly good, if uninteresting, DateListPlot. –  m_goldberg Mar 14 at 4:02
add comment

1 Answer 1

up vote 2 down vote accepted

I make the assumption that your imported data is of the form {{20131217, 123123.}, {20140313, 321321.},...}. If the date is a string then omit the String function in the following. You need to convert your imported into a date recognized by Mathematica.

DateListPlot[{DateList[ToString@#1], #2} & @@@ {{20131217, 
    123123.}, {20140313, 321321.}}, PlotStyle -> PointSize[0.02], 
 DateTicksFormat -> {"MonthNameShort", " ", "Day", ", ", "Year"}, 
 PlotRange -> {0, 400000}]

enter image description here

share|improve this answer
    
I tried this, but received a similar error: DateListPlot[{DateList[ToString@#1], #2} & @@@ data, PlotStyle -> PointSize[0.02], DateTicksFormat -> {"MonthNameShort", " ", "Day", ", ", "Year"}, PlotRange -> {0, 400000}] DateList::str: "String \!(\"{20131217, 114812.}\") cannot be interpreted as a date in format Automatic" DateListPlot::ntdt: The first argument to DateListPlot should be a list of pairs of dates and real values, a list of real values, or a list of several such lists –  sbank Mar 15 at 14:20
    
@sbank this critically depends on your data format. DateList just converts your date into a format Mathematica understands. If this failed try remove ToString as your first argument may be a string. Otherwise a hyerlink to a small sample of your data or copying and pasting your imported data as an update for the question. –  ubpdqn Mar 15 at 14:33
1  
@sbank I have run code on pasted version of data={ {20131217, 114812.}, {20131220, 114192.}, {20131221, 114192.}, {20131223, 114192.}, {20131224, 114504.}, {20131226, 115408.}, {20131227, 115165.} } without problem. Note data should be list. You should remove TableForm. –  ubpdqn Mar 15 at 14:52
    
Thanks! it was the TableForm (or my use of //Short). I thought that those arguments just changed the output for presentation purposes. I did not think it would change the interpretation of the data. Thanks so much. I spent more hours on this than I would like to admit for something so trivial! –  sbank Mar 16 at 14:52
    
@sbank I am glad we have reached a positive resolution. We all have had similar experiences...the site helps pool and share it:) –  ubpdqn Mar 17 at 0:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.