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.

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!

share|improve this question
    
That does look very helpful. I will give it a shot. Thank you! –  Pancholp Dec 13 '12 at 14:13
1  
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
2  
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

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer

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.