Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 621
Proj_UK Avatar asked Sep 14 '11 12:09

Proj_UK


1 Answers

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.

like image 88
WReach Avatar answered Jan 02 '23 20:01

WReach