3

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!

Feyre
  • 8,597
  • 2
  • 27
  • 46
anonymous
  • 433
  • 3
  • 9

2 Answers2

1

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

Table[ 
 ...
 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.. )

george2079
  • 38,913
  • 1
  • 43
  • 110
0
n = 0;(*index for filename*)

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

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

  ,"Table"]
,{a, 0, 6, 2}]

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

Sumit
  • 15,912
  • 2
  • 31
  • 73