1
$\begingroup$

I am calculating a few lists of values that I have in arrays. I want to export these into a tab-delimited text file that I can import into other programs for analysis, plotting and so on but I am struggeling for hours to produce a number format without any gimmicks. I just want regular floating point numbers.

Export[filename,Transpose[{results1, results2, results3}], "TSV"]

That is my naive approach that kind of works. But now some numbers happen to be integer values like 10000. This is printed as

10000.

which can't be read by the program because the decimal part is missing. I found that FortranForm creates nicely usable numbers, but when I try to put that into the expression like

Export[filename,Transpose[{FortranForm[results1], FortranForm[results2], FortranForm[results3}]], "TSV"]

It prints stuff like

Transpose[{List(0.001,0.001584893192461114,0.0025118864315095794,0.003981071705534973, ...

into the file.

The export is done in a loop (filename is dynamically created) so I can't really do it by hand.

So my question is, how can I put regular numbers into a file? It can't be that complicated.


As pointed out in the comment section there is a very similar question already at: How to export data files using specific format

This is about getting a suitable format for Fortran in the exported data. That however is not enough in my case. That format is not completely valid for most other Windows programs. Furthermore the answers there do not treat the problem of where to implement the function.

The answer by andre given there however helps a lot, as I have pointed out in my answer to this question.

$\endgroup$
3
  • $\begingroup$ Read the docs for NumberForm, the results can be tailored to allow for nearly any format in your exported data. $\endgroup$
    – dionys
    Sep 14, 2015 at 12:48
  • $\begingroup$ @dionys I tried to do that at different positions (for each number, for each array, for the whole matrix) and each time the Export function just printed NumberForm[10000.,10] or something like that into the file. It was kind of like my FortranForm example. Sorry for sounding a bit cranky by the way, I'm just completely confused at this point. $\endgroup$
    – Jens
    Sep 14, 2015 at 12:52
  • $\begingroup$ No worries. Always frustrating when things aren't working. Sounds like you may have the format specification for Export set incorrectly. This question covers an issue related to making Export evaluate NumberForm: problem with NumberForm export. $\endgroup$
    – dionys
    Sep 14, 2015 at 12:58

1 Answer 1

3
$\begingroup$

The answers in the duplicate link go into the right direction but would still not suffice.

This is from the answer by andre given here: https://mathematica.stackexchange.com/a/19503/29305

toEString[dat_] := 
 If[dat == 0.,
  "0.0000000000000000+E00",
  MantissaExponent[dat] //
   With[{mantissa = #[[1]], exponent = #[[2]]},
     {
        If[mantissa < 0, "-", ""],
        "0.",
        ToString /@ 
         PadRight[First[RealDigits[mantissa, 10, Automatic]], 16, 0],
        If[exponent < 0, "-E", "+E"],
        ToString /@ IntegerDigits[exponent, 10, 2]
        } //
       Flatten //
      StringJoin] &
  ]

This produces, for some number, the output:

-0.5814759296236481+E05

Close, but it still would not work in most programs since the usual format expects the sign of the exponent to be behind the E. This is easily incorporated there through, by exchanging If[exponent < 0, "-E", "+E"] with If[exponent < 0, "E-", "E"].

Then it correctly produces the output

-0.5814759296236481E05

which can be read by most computer software in Windows.

The function is used on any value before it is added to the result arrays.

$\endgroup$

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