MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

formatted table output to ascii file

For the life of me I just cannot figure out how to get Mathematica to
produce nicely formatted (C or Fortran format) output of a 2D table of
numbers to an ASCII file.

Let's say I've got this table:

{{-6., 0.0000243729, 0.0000759669, 0.00187499},
 {-2., 0.0613641, 0.0827868, 0.0831174},
 {0.4, 0.317162, 0.307924, 0.33977}}

I'd like the resulting file to correspond to this C format string
"%5.2f %10.3e %10.3e %10.3e\n", i.e. to look like this:

-6.00  2.437e-05  7.597e-05  1.875e-03
-2.00  6.136e-02  8.279e-02  8.312e-02
 0.40  3.172e-01  3.079e-01  3.398e-02

I tried Export["file.dat",table], but got this mess:

-6.     0.00002437286669095962  0.0000759669270937278
-1.9999999999999998     0.06136412066687522
0.08278680554897219     0.08311737741506361
0.40000000000000036     0.31716209930168276
0.3079237859649251      0.3397696229714295

I also tried pre-formatting with something like

newdata = Map[ToString[PaddedForm[#, {6, 4}]] &, table, {2}];

but my mathematica-fu isn't good enough to generalize this to the
desired format.

Any help would be greatly appreciated!


  • Prev by Date: Calling functions with huge arguments
  • Next by Date: Problem with mathlink:PLEASE HELP ME
  • Previous by thread: Re: Calling functions with huge arguments
  • Next by thread: Re: formatted table output to ascii file