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 want to read a file which contains seven columns of real numbers. Mathematica seems to have a problem with the high precision I need.

My code:

solFile = OpenRead["sol_Je9"]; 
data = ReadList[solFile, {Real, Real, Real, Real, Real, Real, Real}];
Close[solFile];

The error messages:

General::digit: Digit at position 1 in -239.916104094628030 is too large to be used in base 10. ReadList::readn: Invalid real number found when reading from sol_Je9.

How can I fix this?

share|improve this question
    
Apparently it needs the numbers to be in Fortran format... –  cormullion Oct 5 '13 at 10:59
    
Does Number instead of Real work? –  ssch Oct 5 '13 at 11:01
    
@ssch: Number gives the same error –  physicsGuy Oct 5 '13 at 11:03
4  
@Thomas Can you post the file? –  R Hall Oct 5 '13 at 11:27
2  
Have you considered Import as alternative ReadList? –  m_goldberg Oct 5 '13 at 11:44

1 Answer 1

Try using Record and one of the options of ReadList

data = ReadList["filename", Table[Record, {7}], RecordSeparators -> {"\t", "\n"}]

Then use ToExpression to convert the elements (since they have Head String) to Reals.

ToExpression[data]

Or combine both into one line

 data = ToExpression @ ReadList["filename", Table[Record, {7}], RecordSeparators -> {"\t", "\n"}]

Note that I used tabs ("\t") as one of my RecordSeparators, since you haven't posted the files, so I just copied that number and created a look-alike file. You'll have to read in the first few lines of your file and use FullForm and InputForm to see what RecordSeparators you should use. But this should work.

EDIT For completeness here is how you can use InputForm to view the structure of your file and determine what RecordSeparators to use.

You can Read in the first three lines of your file as follows:

ReadList["filename", String, 3]

In my made up file, here is what I get:

{" -239.916104094628030234   -239.916104094628030543     \
-239.916104094628030233", " -239.916104094628030345  \
-239.916104094628030570  -239.916104094628030355", " \
-239.916104094628030678  -239.916104094628030442     \
-239.916104094628030654"}

Then using InputForm gives:

{" -239.916104094628030234\t -239.916104094628030543\t -239.916104094628030233", 
 " -239.916104094628030345\t -239.916104094628030570\t -239.916104094628030355", 
 " -239.916104094628030678\t -239.916104094628030442\t -239.916104094628030654"}

And you can see that "\t" is visible.

Now using

data = ToExpression@ReadList["filename", Table[Record, {3}], RecordSeparators -> {"\t", "\n"}]

Gives:

   {{-239.916104094628030234, -239.916104094628030543, -239.916104094628030233},  
   {-239.916104094628030345, -239.916104094628030570, -239.916104094628030355}, 
{-239.916104094628030678, -239.916104094628030442, -239.916104094628030654}}

Now, Head[data[[1,1]]] gives Real

share|improve this answer

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.