Proj_UK
Proj_UK

Reputation: 157

Exporting Data from Mathematica with commas

I am exporting data from mathematica in this manner to a file with "dat" extension.

numbercount=0;
exporttable =
   TableForm[
    Flatten[
     Table[
      Table[
       Table[{++numbercount, xcord, ycord, zcord}, {xcord, 0, 100, 5}], 
      {ycord, 0, 100, 5}], 
     {zcord,10, 100, 10}],
    2]];
Export["mydata.dat", exporttable]

Now what happens is the "mydata.dat" file the output appears like this

1  0   0   10
2  5   0   10
3  10  0   10  and so on

But I want the data to appear like this in the "mydata.dat" file.

1, 0,  0,  10
2, 5,  0,  10
3, 10, 0,  10  and so on

If you observer I want a comma after every first,second and third number but not after the fourth number in each line.

I have tried this code it inserts the commas between the number But it takes a long time to run as I have huge amounts of data to be exported.I also feel that someone can perhaps come up with a better solution.

numbercount=0;
exporttable =Flatten[
              Table[
               Table[
                Table[{++numbercount, xcord, ycord, zcord}, {xcord, 0, 100, 5}], 
               {ycord, 0, 100, 5}], 
              {zcord,10, 100, 10}], 
             2];
x = TableForm[Insert[
              exporttable[[i]], ",", {{2}, {3}, {4}}], {i, 1, Length[exporttable]}];
Export["mydata.dat", x]

Upvotes: 3

Views: 1253

Answers (2)

Dr. belisarius
Dr. belisarius

Reputation: 61056

As an aside note, please note that you can build your list with only one Table command, and without explicit index variables:

exporttable1 =  MapIndexed[Join[#2, #1] &, 
                                Flatten[Table[{xcord, ycord, zcord}, 
                                                {zcord, 10, 100, 10}, 
                                                {ycord, 0, 100, 5}, 
                                                {xcord, 0, 100, 5}], 2]]

exporttable1 == exporttable
(*
-> True
*)

Upvotes: 2

rcollyer
rcollyer

Reputation: 10695

Have you tried exporting it as a CSV file? The third parameter of Export is file type, so you'd type

Export["mydata.dat", x, "CSV"]

To add to this, here is a categorical list and an alphabetical list of the available formats in Mathematica.

Upvotes: 9

Related Questions