2
$\begingroup$

I have a function which is written below

f[n_, w_Integer /; Positive[w]] := PaddedForm[N[n], {w, w}, NumberPadding -> {"", "0"}, NumberFormat -> (Row[{#1, "e", If[#3 == "", "0", #3]}] &)];

Then I have defined a matrix as

a = ConstantArray[0, {3, 3}];

My matrix elements are defined as:

a[[1, 1]] = f[3.332567, 4];
a[[1, 2]] = f[2.17530, 4];
a[[1, 3]] = f[0., 4];
a[[2, 1]] = f[2.64254, 4];
a[[2, 2]] = f[1.6432, 4];
a[[2, 3]] = f[5.2533, 4];

Then I would like to export this matrix into a txt file using the command below which is as

Export["a.txt", a, "Table", "FieldSeperators" -> ""]

Instead of obtaining the formatted numbers in the txt file, I get the commands in the text file as:

PaddedForm[3.332567, {4, 4}, NumberPadding -> {"", "0"}, NumberFormat -> (Row[{#1, "e", If[#3 == "", "0", #3]}] & )]    PaddedForm[2.1753, {4, 4}, NumberPadding -> {"", "0"}, NumberFormat -> (Row[{#1, "e", If[#3 == "", "0", #3]}] & )]  
PaddedForm[0., {4, 4}, NumberPadding -> {"", "0"}, NumberFormat -> (Row[{#1, "e", If[#3 == "", "0", #3]}] & )]

PaddedForm[2.64254, {4, 4}, NumberPadding -> {"", "0"}, NumberFormat -> (Row[{#1, "e", If[#3 == "", "0", #3]}] & )] PaddedForm[1.6432, {4, 4}, NumberPadding -> {"", "0"}, NumberFormat -> (Row[{#1, "e", If[#3 == "", "0", #3]}] & )]

I have used a code which is as:

Export["a.txt", OutputForm[a], "Table", "FieldSeperators" -> ""]

However it does not give the solution as table; it gives the solution in curly brackets which I don't want.

{{3.3330e0, 2.1750e0, 0.0000e0}, {2.6430e0, 1.6430e0, 5.2530e0}, {0, 0, 0}}

How could I solve this problem to obtain a txt file which stores data in a table format ?

Thank you.

$\endgroup$

1 Answer 1

5
$\begingroup$
Export["a.txt", Map[OutputForm, a, {2}], "Table", "FieldSeperators" -> ""]

OutputForm can be replaced by ToString.

$\endgroup$
1
  • $\begingroup$ Thank you @xzczd $\endgroup$
    – user45055
    Nov 29, 2019 at 7:35

Your Answer

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

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