I wrote this little function to help me export tables with formatted numbers to files: (By the way, if you have suggestions on how to make it more efficient and compact, it would be great)
Options[exportSimpleFormatTable] = {numberFormat -> NumberForm,
formArgument -> {2, 1}, fortranOutput -> False}
exportSimpleFormatTable[filename_?StringQ, vectorcolumn__,
opts : OptionsPattern[]] :=
Module[{len, list, formfu, formarg, numfun, formattedlist},
list = List@vectorcolumn;
len = Length@list;
list = Thread[list];
numfun = OptionValue[numberFormat];
formarg = OptionValue[formArgument];
formfu = Function[numfun[#, formarg]];
If[OptionValue[fortranOutput] == True,
formfu = Function[ScientificForm[#,
NumberFormat -> (Row[{#1, "e", If[ToString[#3] == "", 0, #3]}] &)]]];
formattedlist = MapAt[formfu, list, {All, All}];
Print[formattedlist];
Export[filename, formattedlist, "Table"]
]
When I use it like this:
zzz = {0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1., 1.1, 1.2, 1.3, 1.4, 1.5}
bbb = {0.992, 0.855, 1.411, 1.083, 0.744, 1.292, 1.363, 1.988, 1.111,
0.7112, 1.573, 1.1423}
Everything works fine:
exportSimpleFormatTable["test.dat", zzz, bbb, 2*zzz,
fortranOutput -> False]
(* {{0.4,1.0,0.8},{0.5,0.9,1.0},{0.6,1.4,1.2},{0.7,1.1,1.4},{0.8,0.7,1.6},{0.9,1.3,1.8},{1.0,1.4,2.0},{1.1,2.0,2.2},{1.2,1.1,2.4},{1.3,0.7,2.6},{1.4,1.6,2.8},{1.5,1.1,3.0}} *)
FilePrint["test.dat"]
0.4 1.0 0.8
0.5 0.9 1.0
0.6 1.4 1.2
0.7 1.1 1.4
However when I set the fortranOutput option to True, I get:
exportSimpleFormatTable["test.dat", zzz, bbb, 2*zzz, fortranOutput -> True]
{{4.e-1,9.92e-1,8.e-1},{5.e-1,8.55e-1,1.e0},{6.e-1,1.411e0,1.2e0},{7.e-1,1.083e0,1.4e0},{8.e-1,7.44e-1,1.6e0},{9.e-1,1.292e0,1.8e0},{1.e0,1.363e0,2.e0},{1.1e0,1.988e0,2.2e0},{1.2e0,1.111e0,2.4e0},{1.3e0,7.112e-1,2.6e0},{1.4e0,1.573e0,2.8e0},{1.5e0,1.1423e0,3.e0}}
But FilePrint
gives me unevaluated things:
FilePrint["test.dat"]
ScientificForm[0.4, NumberFormat -> (Row[{#1, "e", If[ToString[#3] == "", 0, #3]}] & )] ScientificForm[0.992, NumberFormat -> (Row[{#1, "e", If[ToString[#3] == "", 0, #3]}] & )] ScientificForm[0.8, NumberFormat -> (Row[{#1, "e", If[ToString[#3] == "", 0, #3]}] & )]
ScientificForm[0.5, NumberFormat -> (Row[{#1, "e", If[ToString[#3] == "", 0, #3]}] & )] ScientificForm[0.855, NumberFormat -> (Row[{#1, "e", If[ToString[#3] == "", 0, #3]}] & )] ScientificForm[1., NumberFormat -> (Row[{#1, "e", If[ToString[#3] == "", 0, #3]}] & )]
So, there is a difference between the internal Print
and the Export
.
However when I use this accepted answer: Problem with NumberForm when exporting, which suggests wrapping ScientificForm
with OutputForm
,
I get something even weirder:
exportSimpleFormatTable[filename_?StringQ, vectorcolumn__,
opts : OptionsPattern[]] :=
Module[{len, list, formfu, formarg, numfun, formattedlist},
list = List@vectorcolumn;
len = Length@list;
list = Thread[list];
numfun = OptionValue[numberFormat];
formarg = OptionValue[formArgument];
formfu = Function[numfun[#, formarg]];
If[OptionValue[fortranOutput] == True,
formfu =
Function[
OutputForm@
ScientificForm[#,
NumberFormat -> (Row[{#1, "e",
If[ToString[#3] == "", 0, #3]}] &)]]];
formattedlist = MapAt[formfu, list, {All, All}];
Print[formattedlist];
Export[filename, formattedlist, "Table"]
]
exportSimpleFormatTable["test.dat", zzz, bbb, 2*zzz,
fortranOutput -> True]
{{4.e-1,9.92e-1,8.e-1},{5.e-1,8.55e-1,1.e0e0},{6.e-1,1.411e0e0,1.2e0e0},{7.e-1,1.083e0e0,1.4e0e0},{8.e-1,7.44e-1,1.6e0e0},{9.e-1,1.292e0e0,1.8e0e0},{1.e0e0,1.363e0e0,2.e0e0},{1.1e0e0,1.988e0e0,2.2e0e0},{1.2e0e0,1.002e2,2.4e0e0},{1.3e0e0,7.112e-1,2.6e0e0},{1.4e0e0,1.573e0e0,2.8e0e0},{1.5e0e0,1.1423e0e0,3.e0e0}}
I get this strange e0e0's which don't happen for e-1 (or other exponents as I have checked)
But the FilePrint
now works fine (although with that strange error in e0e0):
FilePrint["test.dat"]
4.e-1 9.92e-1 8.e-1
5.e-1 8.55e-1 1.e0e0
6.e-1 1.411e0e0 1.2e0e0
7.e-1 1.083e0e0 1.4e0e0
8.e-1 7.44e-1 1.6e0e0
9.e-1 1.292e0e0 1.8e0e0
1.e0e0 1.363e0e0 2.e0e0
1.1e0e0 1.988e0e0 2.2e0e0
1.2e0e0 1.002e2 2.4e0e0
1.3e0e0 7.112e-1 2.6e0e0
1.4e0e0 1.573e0e0 2.8e0e0
1.5e0e0 1.1423e0e0 3.e0e0
Does somebody have an idea what is going here?? I tried to use different forms of Map
, Thread
, and so on, but nothing works. Also other solutions here on SE don't solve my problem.