1
$\begingroup$

Assuming I already have the file created.

I have a programme that,returns an output, in our case number. I want Mathematica to save this number in a file, together with results saved from previous calculations.

After having checked Mathematica commands, I found that I could do something like:

file = OpenWrite["test.dat"];
Write[file, new];
Close[file];

However, this doesn't work, because it overwrites the information that is already in the file. Working with this limitation, I tried a different approach:

results=Import["test.dat", "Table"];
AppendTo[results,newresult];
file = OpenWrite["test.dat"];
Write[file,results];
Close[file];

This new approach still doesn't work since when I initialize the file test.dat with

Export["test.dat", Range[1, 10]]

and then import the list, I do not get the same list, but

{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}}

I get the same unwanted list, even if I add the "Table" option to Export, or Import...

How do I efficiently save the progress of my programme in the file test.dat?

Any help would be appreciated.

$\endgroup$
  • 1
    $\begingroup$ See Put and PutAppend $\endgroup$ – Marius Ladegård Meyer Nov 20 '14 at 12:18
  • $\begingroup$ Related: "The best way to construct a function with memory." $\endgroup$ – Alexey Popkov Nov 20 '14 at 12:25
  • $\begingroup$ @MariusLadegårdMeyer The PutAppend also seems to overwrite the info, and doesn't solve the import data problem $\endgroup$ – An old man in the sea. Nov 20 '14 at 12:29
  • 1
    $\begingroup$ @Anoldmaninthesea, try this: (# >>> "test.dat") & /@ {1, 2, 3} followed by ReadList["test.dat"]. For me, this returns {1,2,3}. If you now want to add some new numbers, say (# >>> "test.dat") & /@ {4, 5, 6, 7, 8}, you will find that ReadList["test.dat"] gives you {1,2,3,4,5,6,7,8}. If you only need to save numbers/lists, and not expressions, then this is the simplest way I can think of. $\endgroup$ – Marius Ladegård Meyer Nov 20 '14 at 12:41
  • 1
    $\begingroup$ @Anoldmaninthesea, I can't reproduce that. Did you save it both times with >>>? Try starting from a new file name. $\endgroup$ – Marius Ladegård Meyer Nov 20 '14 at 13:25
2
$\begingroup$

For simply saving numbers or lists to file, I personally prefer to use Put/>> and PutAppend/>>> to store data, and ReadList to retrieve it. For instance:

(# >>> "file.dat") & /@ {1,2,3};
ReadList["file.dat"]
(* {1,2,3} *)
(# >>> "file.dat") & /@ {1,2,3,4,5};
ReadList["file.dat"]
(* {1,2,3,1,2,3,4,5} *)

You could obviously also store the lists each time and then Join them if you want:

{1, 2, 3} >>> "file.dat";
{4, 5, 6, 7} >>> "file.dat";
Join @@ ReadList["file.dat"]
(* {1, 2, 3, 4, 5, 6, 7} *)
$\endgroup$
5
$\begingroup$

Open the file with OpenAppend not OpenWrite. Any Writes will then append to the file not overwrite.

$\endgroup$
  • $\begingroup$ this is especially useful in combination with ExportString for text like formats where you can append line by line, e.g. CSV. $\endgroup$ – Albert Retey Nov 21 '14 at 8:21
  • $\begingroup$ Many thanks for your answer Ymareth $\endgroup$ – An old man in the sea. Nov 21 '14 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.