# Export different simulation parameters and results to same file

I have a simulation, and I would like to export the parameters and results to a same spreadsheet without overwriting the previous values.

if I have:

parameter1 = {0.1};
parameter2 = {0.2};
result1 = {10};
result2 = {20};


And if I export it I get that. But then if I change the parameters and run the simulation again, I would like these new values to go right after the ones I had previously exported.

parameter1 = {0.1,0.3};
parameter2 = {0.2,0.2};
result1 = {10,25};
result2 = {20,30};


As a spreadsheet or a list with headings would be best. I saw an answer to a similar question recommended creating a spreadsheet beforehand and then defining the cells where the values should be exported to, but this would still rewrite the cells. I guess I could always manually change the location after each simulation. Maybe mathematica has some built-in function to do something like this? I am still very new, I apologize if this question is elementary or doesn't make sense. Any help is much appreciated!

-
That does look very helpful. I will give it a shot. Thank you! –  Pancholp Dec 13 '12 at 14:13
You might also want to look at PutAppend ( short form >>> ), which can append expressions to files : {param1,param2,res1,res2}>>>"myfile" –  image_doctor Dec 13 '12 at 14:19
Please see this answer too. –  Szabolcs Dec 13 '12 at 15:07
I knew there had to be a simple way to do this. Thanks @Szabolcs! –  Pancholp Dec 13 '12 at 22:11
@image_doctor, Not sure I understand in which case PutAppend would be better than OpenAppend or viceversa, but I'll try with both to find out. Thanks for your help! –  Pancholp Dec 13 '12 at 22:11

Following the comments above, this is what I did:

First I opened a document and made the appropriate headings

    $stream = OpenWrite["simulationdata.tsv", BinaryFormat -> True]; WriteString[$stream, "Data from simulation\n"]
Export[$stream, Table[{"parameter1", "parameter2", "result1", "result2"}, {1}], "TSV"] WriteString[$stream, "\n"]
Close[$stream]  Then I used OpenAppend to add the data to the same file after each simulation $stream = OpenAppend["simulationdata.tsv", BinaryFormat -> True];
Export[$stream, Table[{parameter1, parameter2, result1, result2}, {1}], "TSV"] WriteString[$stream, "\n"]
Close[\$stream]


And I can use Import to check how the data looks like. This seems to keep the data in neat columns, and that is what I needed.

Import["simulationdata.tsv", "Text"]


Thanks for your help. Suggestions for improvement are always welcome.

-