I have a complicated function and my computer crashes when I try to plot it using ListPlot from 0 to 30. I think it crashes because it runs out of memory.

So what I want to do is to use Export["file",list,"Table"] to save each iteration.

For example, I use Cos(x)

Export["trial1", Table[{N[x], N[Cos[x]]}, {x, -π, 0, π/100}], "Table"]
Export["trial2",Table[{N[x], N[Cos[x]]}, {x, 0, π, π/100}], "Table"]

Now I'm not sure whether Mathematica will save each iteration into a text file or it will first finish all the iterations before it saves everything into a text file.

How do I make Mathematica save each iteration into a text file?

Thanks in advance!

  • 2
    $\begingroup$ The innermost function is run first. $\endgroup$ – Feyre Oct 17 '16 at 8:34
  • 1
    $\begingroup$ Have you seen this method for using file backed data? $\endgroup$ – rcollyer Oct 17 '16 at 20:07

assuming you really want to write everything to a single file, you can do like this:

 f = OpenAppend["test.txt"];
 WriteString[f,N[x]," ",N[Cos[x]],"\n"];
 Close[f]; ,  {x, -π, 0, π/100} ]

or if you want to utilize Export formatting do like this:

 WriteString[f, ExportString[{{N@x, N@Cos[x]}}, "Table"] <> "\n" ]

note this is usually not a good way to export data, but for this situation where you want to salvage everything up to the point of a crash it will do the job. Closing and re-opening on every iteration is intended to force a file buffer flush... ( Why mathematica has no Flush is a good question )

( you probably want to DeleteFile first in case it already exists.. )

  • $\begingroup$ I tried your suggestion WriteString[f, ExportString[{{N@, N@Cos[i]}}, "Table"] <> "\n" ] but there seems to be an error $\endgroup$ – PhilCsar Oct 18 '16 at 0:36
  • $\begingroup$ Also, Im not familiar at using Export in Mathematica and I can't understand the first answer. :D $\endgroup$ – PhilCsar Oct 18 '16 at 1:29
  • $\begingroup$ there was a typo, try it now $\endgroup$ – george2079 Oct 18 '16 at 1:45
  • $\begingroup$ its working now. thanks! :D $\endgroup$ – PhilCsar Oct 18 '16 at 1:49
n = 0;(*index for filename*)

Table[n++; Export[ToString[n] <> ".dat",

  Table[{x, Sin[x]}, {x, a, a + 2, 0.1}](*data to export*)

,{a, 0, 6, 2}]

{"1.dat", "2.dat", "3.dat", "4.dat"}


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.