1
$\begingroup$

Suppose I have a simple loop

dat=Table["i",{i,1,10}];

Now once the run is finished, I can store the data using

Export["dat.txt",dat]

It works perfectly. However suppose I have a bigger loop with complicated evaluations which takes long time to complete. And I need to use the output upto i=5 before the run is complete. What I mean is that data should be stored in "dat.txt", when i=1 is complete. Then in the next line the file includes the output for i=2. This helps me to use the output file even if some problem related to computer hang problem appears and the code finishes prematurely.

Can someone enlighten me regarding the method I should use for that?

$\endgroup$
1
  • $\begingroup$ does not work for multiple columns, also the output format given is inside inverted comma, I can't use those further. $\endgroup$ Feb 10 at 15:29
2
$\begingroup$

In the Table, wrap your output in e.g. PutAppend so each time an output is generated it gets appended to a file. I recommend that you label the results with the value of the iterator as well.

Here is a minimal example, where I use Pause to simulate a long computation, and Print just to provide visual feedback to you of the progress of the computation (neither would be needed in your code):

Table[
 PutAppend[
  Print[i]; Pause[10]; {i, "results from iteration " <> ToString[i]},
  "runningOutput.txt"
 ],
 {i, 0, 5}
]

If you open the file in an editor such as Notepad++, you will be notified of the changes to the file and see the updates in real time.

Note that it would be up to you (or to extra code that you can write) to clear the file before each run, if you want to.

$\endgroup$

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