Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In the following code "FieldSeparators"->" " prints a lot of quotation marks in the output file, instead of just spaces. What's wrong with it?

SetDirectory[NotebookDirectory[]];
s = 30; sy = s;
nmin = 0; nmax = 3; xmin = 0; xmax = 3;

out = Flatten[
        Table[{N[n] // ToString[PaddedForm[#, {6, 5}, NumberPadding -> {" ", " "}]] &,
               N[x] // ToString[PaddedForm[#, {6, 5}, NumberPadding -> {" ", " "}]] &, 
               N[BesselJ[n, 3 x]] /. {
                  Indeterminate -> "nan", ComplexInfinity -> "nan", Infinity -> "inf"} //
                  ToString[ PaddedForm[#, {6, 5}, NumberPadding -> {" ", " "}]] &},
              {n, nmin, nmax, (nmax - nmin)/(s - 1)}, 
              {x, xmin, xmax, (xmax - xmin)/(sy - 1)}], 1];

$file = OpenWrite["bessel.txt"]; 

Export[$file, out[[1 ;; s]], "Table", "FieldSeparators" -> " "];

For[ i = 2, i <= s, i++, 
     WriteString[$file, "\n\n"] 

     Export[$file, out[[1 + (i - 1)*s ;; i*s]], "Table", "FieldSeparators" -> " "]]

Close[$file];
share|improve this question
    
A double-space seems to work OK. Odd... –  cormullion Nov 9 '13 at 13:49
    
@cormullion not fore me :-( –  Luigi Nov 9 '13 at 15:02

4 Answers 4

up vote 1 down vote accepted

I suppose it is a bug, since any other characters apart from space work as expected. By thye way, in your input some semicolons are missing, after WriteString and before Close.

What you might to is to export with Tab separation (either "FieldSeparators" -> "\t" or no options, since it should be the default) and then convert to spaces with:

Export["bessel.txt",
    StringReplace[
        Import["bessel.txt", "Text"],
        "\t" -> " "
    ],
    "Text"
]
share|improve this answer

I suggest simplifying your code by generating the output table as a single string, completely formated in the way you want it appear in your text file. Then you can bypass fussing with the file writing. The modifications that need to be made to your code are small.

SetDirectory[NotebookDirectory[]];
s = 30; sy = s;
nmin = 0; nmax = 3; xmin = 0; xmax = 3;
i = 0;
out = 
  StringJoin @@ 
    Flatten @
      Table[{
        N[n] // ToString[PaddedForm[#, {6, 5}, NumberPadding -> {" ", " "}]] &,
        N[x] // ToString[PaddedForm[#, {7, 5}, NumberPadding -> {" ", " "}]] &,
        N[BesselJ[n, 3 x]] /. 
          {Indeterminate -> "nan", ComplexInfinity -> "nan", Infinity -> "inf"} // 
            ToString[PaddedForm[#, {7, 5}, NumberPadding -> {" ", " "}]] &,
        If[Mod[++i, s] == 0, "\n\n", "\n"]},
     {n, nmin, nmax, (nmax - nmin)/(s - 1)}, 
     {x, xmin, xmax, (xmax - xmin)/(sy - 1)}];
Export["bessel.txt", out]

The resulting file will look like this:

 0.      0.       1.     
 0.      0.10345  0.97607
 0.      0.2069   0.90598
 0.      0.31034  0.79476
...
 0.      2.89655 -0.00973
 0.      3.      -0.09033

 0.10345 0.       0.     
 0.10345 0.10345  0.84929
 0.10345 0.2069   0.8531 
 0.10345 0.31034  0.79121
...
share|improve this answer
    
I feel awkward because I appreciate your solution very much, but I'm going to accept @user8074's answer since it preserves the original table. Thank you very much anyway. –  Luigi Nov 15 '13 at 15:34

There's something going on I don't understand - here's a simpler example:

pad[n_] := ToString[
  PaddedForm[n, {6, 5}, NumberPadding -> {" ", " "}]];
out = Table[pad@N[BesselJ[n, n x]] , {n, 1, 5}, {x, 1, 5}]

table output

ExportString[out, "Table", "FieldSeparators" -> " "] (* one space *)

export 1

ExportString[out, "Table", "FieldSeparators" -> "  "] (* two spaces *)

export 2

share|improve this answer

Does using "TSV" file format suit your needs? Further, ShowStringCharacters->False can be used to suppress the string characters in output. This did not seem to work consistentl with "FieldSeparator" option for Table.

Modified end of your code:

$file = OpenWrite["bessel.txt"];

Export[$file, out[[1 ;; s]], "TSV", ShowStringCharacters -> False];

For[i = 2, i <= s, i++, 
 WriteString[$file, "\n\n"] Export[$file, out[[1 + (i - 1)*s ;; i*s]],
    "TSV", ShowStringCharacters -> False]]

Close[$file];
share|improve this answer
    
It seems to me that this way I get the same result as without FieldSeparators or as with "FieldSeparators"->"\t" –  Luigi Nov 10 '13 at 15:19
    
@Luigi Sorry if this was unhelpful. I tried the code and got a tab separated table without string characters. I could not ShowStringCharacters->False to suppress the string characters with other attempts. –  ubpdqn Nov 11 '13 at 1:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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