5
$\begingroup$

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?

$\endgroup$
7
  • $\begingroup$ Apparently it needs the numbers to be in Fortran format... $\endgroup$
    – cormullion
    Oct 5, 2013 at 10:59
  • $\begingroup$ Does Number instead of Real work? $\endgroup$
    – ssch
    Oct 5, 2013 at 11:01
  • $\begingroup$ @ssch: Number gives the same error $\endgroup$
    – physicsGuy
    Oct 5, 2013 at 11:03
  • 4
    $\begingroup$ @Thomas Can you post the file? $\endgroup$ Oct 5, 2013 at 11:27
  • 2
    $\begingroup$ Have you considered Import as alternative ReadList? $\endgroup$
    – m_goldberg
    Oct 5, 2013 at 11:44

1 Answer 1

6
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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