1
$\begingroup$

I have seen the answer How to export data in scientific form but it is only partly helping me.

I have the list of data which I want to export in a formatted form to a file. In addition I want to prepend a header.

What I tried:

list = {0.004,0.00273904,0.00447323,0.00344523,0.00327233,0.00375527,0.055290,0.008,0.00273586,0.00446845,0.00344505,0.00327311,0.00376325,0.06050960,0.012,0.00273243,0.00446187,0.00344387,0.00327414,0.00377089,0.06586520,0.016,0.0027274,0.00445676,0.00344366,0.00327586,0.00377908,0.07112480,0.02,0.00272475,0.004451,0.00344402,0.0032789,0.0037872,0.07636090,0.024,0.0027227,0.00444807,0.00344451,0.00328181,0.0037949,0.081729};

formatted = Partition[Map[NumberForm[#, {12, 9}] &, list], 7];
header = {"       time          x1          y1          x2          y2          x3          y3"};
output = Prepend[formatted, header];

Export["data.txt", output, "Table"];

The content of the file is:

       time          x1          y1          x2          y2          x3          y3
0.004000000 0.002739043 0.004473226 0.003445232 0.003272333 0.003755268 0.05529000
0.008000000 0.002735863 0.004468450 0.003445050 0.003273114 0.003763252 0.06050960
0.012000000 0.002732431 0.004461866 0.003443870 0.003274136 0.003770894 0.06586520
0.016000000 0.002727400 0.004456763 0.003443659 0.003275864 0.003779077 0.07112480
0.020000000 0.002724752 0.004450998 0.003444017 0.003278901 0.003787201 0.07636090
0.024000000 0.002722704 0.004448069 0.003444513 0.003281807 0.003794901 0.08172900

I have two questions:

a. How can I prepend the header more elegantly?

b. How can I export the data in exponential form whereby each number should have the same form (same number of digits before the comma, another same number of digits after the comma) e.g.:

0.004000000 -> 4.000000E-3

...

0.081729000 -> 8.172900E-2 

I expect that there must be a Mathematica built in solution to do the upper formatting instead of writing own functions.

$\endgroup$
3
  • $\begingroup$ Maybe Dataset can help you $\endgroup$ Commented Oct 19, 2016 at 10:35
  • 1
    $\begingroup$ related mathematica.stackexchange.com/q/19387/2079 $\endgroup$ Commented Oct 19, 2016 at 10:48
  • $\begingroup$ @george2079: May be I am missing something: with your answering code in the upper link I cannot convert 0.00711248 to 7.11248E-3. $\endgroup$ Commented Oct 19, 2016 at 12:40

1 Answer 1

3
$\begingroup$

actually I think this is all you need.

NumberForm[0.081729000, {6, 6},
     NumberSigns -> {"-", " "},
     ExponentFunction -> (# &), 
     NumberFormat -> (Row[{#1, "E", #3}] &)]

8.172900E-2

I think the first number in {6,6} does nothing here by the way.

If you really want everything aligned regardless of signs of exponents it takes a bit more effort:

StringPadLeft["", 1];  (*this must be evaluated first for no evident reason*)
fixedform[numd_, data_] := Module[{ef},
  ef[s_String /; StringTake[s, 1] == "-"] := 
   "-" <> StringPadLeft[StringTake[s, {2, -1}], 2, "0"];
  ef[s_String] := "+" <> StringPadLeft[s, 2, "0"];
  NumberForm[data, {numd, numd},
   ExponentFunction -> (# &), 
   NumberSigns -> {"-", " "}, 
   NumberFormat :> (Row[{StringPadRight[#1, numd + 2, "0"], "E", 
        ef[#3]}] &)]]

 fixedform[6, 
   Grid[RandomReal[{-20, 20}, {5, 5}] 10^
      RandomReal[{-20, 20}, {5, 5}] ]]

enter image description here

Edit: pre-loading StringPadLeft is needed due to this bug: Strange Error on Fresh Kernel (10.2)

$\endgroup$
2
  • $\begingroup$ This is great ... thanks a lot. When I call fixedform[6, list] (my data above) everything is perfect, only always a minus sign appears. $\endgroup$ Commented Oct 19, 2016 at 15:36
  • 1
    $\begingroup$ oops, I had NumberSigns reversed, should be: NumberSigns -> {"-", " "} $\endgroup$ Commented Oct 19, 2016 at 15:48

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.