3
$\begingroup$

I have this code:

fortranFormDP[x_] := 
  NumberForm[x, {17, 16}, 
   NumberFormat -> (StringJoin[{#1, "d", #3} /. 
        "d" :> "d0" /; #3 == ""] &)];
normals={{-0.14609, -0.0840416, 0.985695}, {-0.213989, 0.112374, 
  0.970351}, {-0.147526, -0.00175264, 0.989057}};
data1 = fortranFormDP /@ normals

My goal is to export the List data1 to a file in a "Table" format. Apparently, it is not straightforward as:

Export["myfile", data1]

because this produces:

{-0.1460901681201728d0, -0.0840415712588872d0, 0.9856950223466470d0}
{-0.2139894236453848d0, 0.1123736099765570d0, 0.9703508119998420d0}
{-0.1475262293814966d0, -0.0017526366367021d0, 0.9890565908527670d0}

Which is definitely not "Table" format. Although I did not find it anywhere, it looks like that Mathematica is unable to generate "Table" format from strings. What would probably work is to convert each sublist to one string separated by spaces or tabs. But there is a problem, if I do:

StringJoin/@data1

I get errors. (My plan was to do StringJoin[Riffle[data1," "]]). The source of the errors is probably due to data1//FullForm. I tried:

StringJoin/@(Evaluate@data1)

but it does not help. How would I force evaluation of data1? And is there an easier way to export "Table" format if the data is strings?

$\endgroup$

1 Answer 1

4
$\begingroup$

the problem is NumberForm is not a string .. here I've added ToString

fortranFormDP[x_] := 
  ToString@NumberForm[x, {17, 16}, 
    NumberFormat -> (StringJoin[{#1, "d", #3} /. 
         "d" :> "d0" /; #3 == ""] &)];
normals = {{-0.14609, -0.0840416, 0.985695}, {-0.213989, 0.112374, 
    0.970351}, {-0.147526, -0.00175264, 0.989057}};

now we need to take care to apply to individual numbers, and also specify "Table" on the Export

data1 = Map[fortranFormDP, normals, {2}]
Export["test.txt", data1, "Table"]

you can also do SetAttributes[fortranFormDP, Listable] , then use your original form:

data1 = fortranFormDP /@ normals

or even data1 = fortranFormDP@normals

$\endgroup$

Your Answer

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

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