Exporting, Appending/Prepending data and text into files (Mathematica)
Asked Answered
S

2

7

I am exporting data from the table "mydata1" in a CSV format into "file1.dat". Below is the mathematica code:

mydata1=TableForm[Flatten[
Table[Table[Table[
                 {xcord, ycord, zcord}, {xcord, 0,50,10}],
                   {ycord,0,50,10}], {zcord, 50, 100, 10}], 2]];

Export["file1.dat",mydata1,"CSV"]

Now my "file1.dat" looks like this:

0,0,50
10,0,50
20,0,50
..
.. and so on

Now I have another set of data from the table "mydata2"(Code given below).I want to able to store the data from this table "mydata2" into same file "file1.dat".But before I do that I need to write a text in the file"file1.dat" for eg"Data below are from mydata2".

Note both data from both tables needed to be exported in CSV format.

mycounter=20
mydata2=TableForm[Flatten[
Table[Table[Table[
                 {++mycounter,xcord, ycord, zcord}, {xcord, 0,50,10}],
                   {ycord,0,50,10}], {zcord, 50, 100, 10}], 2]];

in the end my data file "file1.dat" should look like this

*Data from data from mydata1
0,0,50
10,0,50
20,0,50
... and so on
*Below data from mydata2
21,0,0,50
22,10,0,50
23,20,0,50
... and so on.

If you observe the final data file "file1.dat" should have data from table"mydata2" below the data from"mydata1" and in between there is some text written.

Note: I am willing to export the data with the extension TXT but in CSV format For example:

Export["file1.txt", mydata1, "CSV"]

I have used the "PutAppend" but it dosen't give me the desired results.Either I am not using it properly or perhaps it is not the keyword for my kind of problem.

I have lot of question regarding exporting but I wouldn't ask all of this now since I don't want to confuse you all.

Sylph answered 14/9, 2011 at 12:34 Comment(0)
U
3

Perhaps something like:

mydata1 = 
  Flatten[Table[Table[Table[
          {xcord, ycord, zcord}, 
          {xcord, 0, 20, 10}], {ycord, 0, 20, 10}], {zcord, 50, 50, 10}], 2];
mycounter = 20
mydata2 = 
  Flatten[Table[Table[Table[
          {++mycounter, xcord, ycord, zcord}, 
          {xcord, 0, 20, 10}], {ycord, 0, 20, 10}], {zcord, 50, 50, 10}], 2];

Export["c:\\test.txt", 
      Join[{"* data1 follows"}, mydata1, {"* data2 follows"}, mydata2], "CSV"]

The resulting file is:

* data1 follows
0,0,50
10,0,50
20,0,50
0,10,50
10,10,50
20,10,50
0,20,50
10,20,50
20,20,50
* data2 follows
21,0,0,50
22,10,0,50
23,20,0,50
24,0,10,50
25,10,10,50
26,20,10,50
27,0,20,50
28,10,20,50
29,20,20,50
Upland answered 14/9, 2011 at 13:21 Comment(1)
Thanks for the answer, I have used your answer since it fitted my requirementSylph
V
13

Streams are useful for this purpose. If your goal is to generate mydata1 and mydata2 at different times, then you could do the following. First, let's create the file containing mydata1:

$stream = OpenWrite["file.dat", BinaryFormat -> True];
WriteString[$stream, "*Data from data from mydata1\n"]
Export[$stream, mydata1, "CSV"]
WriteString[$stream, "\n"]
Close[$stream]

We can verify the contents of the file using Import:

In[10]:= Import["file.dat", "Text"]

Out[10]= *Data from data from mydata1
         0,0,50
         10,0,50
         20,0,50
         ...

Now, let's append mydata2 to the end of the file:

$stream = OpenAppend["file.dat", BinaryFormat -> True];
WriteString[$stream, "*Below data from mydata2\n"]
Export[$stream, mydata2, "CSV"]
WriteString[$stream, "\n"]
Close[$stream]

... and again, verify:

In[20]:= Import["file.dat", "Text"]

Out[20]= *Data from data from mydata1
         0,0,50
         10,0,50
         20,0,50
         ...
         *Below data from mydata2
         21,0,0,50
         22,10,0,50
         23,20,0,50
         ...

If desired, it is also possible to write mydata1 and mydata2 into the file at the same time:

$stream = OpenWrite["file.dat", BinaryFormat -> True];
WriteString[$stream, "*Data from data from mydata1\n"]
Export[$stream, mydata1, "CSV"]
WriteString[$stream, "\n*Below data from mydata2\n"]
Export[$stream, mydata2, "CSV"]
WriteString[$stream, "\n"]
Close[$stream]

Note that the BinaryFormat -> True option is used each time we open the stream. This is to disable any automatic newline insertion by the stream write operations. If this option is omitted, some unwelcome double-spacing occurs in the output file.

Verile answered 14/9, 2011 at 14:21 Comment(0)
U
3

Perhaps something like:

mydata1 = 
  Flatten[Table[Table[Table[
          {xcord, ycord, zcord}, 
          {xcord, 0, 20, 10}], {ycord, 0, 20, 10}], {zcord, 50, 50, 10}], 2];
mycounter = 20
mydata2 = 
  Flatten[Table[Table[Table[
          {++mycounter, xcord, ycord, zcord}, 
          {xcord, 0, 20, 10}], {ycord, 0, 20, 10}], {zcord, 50, 50, 10}], 2];

Export["c:\\test.txt", 
      Join[{"* data1 follows"}, mydata1, {"* data2 follows"}, mydata2], "CSV"]

The resulting file is:

* data1 follows
0,0,50
10,0,50
20,0,50
0,10,50
10,10,50
20,10,50
0,20,50
10,20,50
20,20,50
* data2 follows
21,0,0,50
22,10,0,50
23,20,0,50
24,0,10,50
25,10,10,50
26,20,10,50
27,0,20,50
28,10,20,50
29,20,20,50
Upland answered 14/9, 2011 at 13:21 Comment(1)
Thanks for the answer, I have used your answer since it fitted my requirementSylph

© 2022 - 2024 — McMap. All rights reserved.