I am stuck on a problem. I have outputs generated at every iteration of a do loop. However, I want to save the outputs in some other file format, like text file etc, in such a way that i must be able to use any output from iteration later. But the more I am searching about it, the more I am getting confused. Please guide in this respect. Like I read the options 'import', 'export', 'sow', 'reap', 'get' etc but i need simple description. Please guide me in this respect.
I quote the example as
I have some initial

$a$ as $V0 +\Delta V*\Delta t/T$. Then I update the value of $\Delta t $ by having an increment of value $T/ntmax$ and define $b$ as $V0 +\Delta V*\Delta t/T$ . And then I have to multiply both $a$ and $b$ as $Exp[b]*Exp[a]$
I have to repeat this process until $\Delta t $ $/T$ reaches equal to $T$, as shown in the below loop.

 a = V0 + ΔV*Δt/T;
 Δt = Δt + T/ntmax;
 b = V0 + ΔV*Δt/T;
 f = Exp[b]*Exp[a],
 {Δt, T/ntmax, T, T/ntmax}]

As during each iteration I will obtain the new value of $f$. I have to plot few values of $f$. However, I want to keep the record of all, so that I may be able to plot any one of them.

  • 1
    $\begingroup$ Do[Export["test" <> ToString[i] <> ".txt", i^2], {i, 1, 5}] creates 5 files test1.txt, test2.txt etc each containing a single number 1, 4, 9, etc. $\endgroup$
    – bill s
    Jun 2 '14 at 16:53
  • 2
    $\begingroup$ Put is also useful. Your question is very broad though. A more specific question (with examples and preferrably code) will get better answers. $\endgroup$
    – Yves Klett
    Jun 2 '14 at 17:31
  • $\begingroup$ in such a way that i must be able to call the output of any iteration step in some other program I am having hard time understanding what this mean. May be MWE will help. $\endgroup$
    – Nasser
    Jun 2 '14 at 18:52
  • 2
    $\begingroup$ Please use mathematica input form.. For this simple example simply replacing Do with Table will generate a list of sucessive f values. $\endgroup$
    – george2079
    Jun 2 '14 at 20:01
  • 1
    $\begingroup$ @Mehdi , yours is an example where the loop execution time is so long that the file write time is not significant. $\endgroup$
    – george2079
    Jun 3 '14 at 11:06

This is what I do usually to save my result:

SetDirectory["Directory"](* Sets your directory to a folder you want*)
file = OpenWrite["file.dat", FormatType -> OutputForm](* Opens a file called file.dat *)
F[i](* calculates what you like as a function of i *)
Write[file, F[i]]; (* writes F[i] in ith line of the file*)

In your case, you calculate $f=\exp(a)\exp(b)$ at each iteration. Like the above, before closing Do loop, write:


I think that is all you need. If you want to plot $f(t)$ versus $t$ later, it is better to write both in the file:

Write[file,t," ",f];

This command writes $t$ and $f$ of each iteration with a space in between.

  • $\begingroup$ Thanks a lot. So what I understood is that in this way I can save the outputs in .dat format. Right? Now how can I use any of these outputs in my Mathematica window, to plot or perform some other operations on it. $\endgroup$
    – zenith
    Jun 2 '14 at 22:26
  • $\begingroup$ Yes, the result will be in a file named file.dat. After you successfully saved everything inside this file, you can use import to have an array and then use ListPlot (for example). $\endgroup$
    – Mahdi
    Jun 2 '14 at 22:38
  • 1
    $\begingroup$ this helped to save my data and retrieve while using do loop. Many thanks $\endgroup$
    – zenith
    Jun 3 '14 at 16:33

If you're only using hundreds of iterations, then Table as @george2079 suggests in a comment is the simplest way to go. If you're looking at millions of iterations, then using the vectorized property of the built-in functions is more efficient. Below, 100 million values are computed in a little over 3 seconds (Mac 2.7GHz 16GB i7):

V0 = 1.;
ΔV = 2.;
T = 5;
ntmax = 10^8;
data = Evaluate[Simplify[ Exp[V0 + ΔV*#/T] * Exp[V0 + ΔV*(# + T/ntmax)/T] ]] &@
   Range[N@T/ntmax, T, T/ntmax]; // AbsoluteTiming

(* {3.216403, Null} *)

Simplifying this particular expression reduces the computation time to about 1/3 of the time it takes the unsimplified expression.

A smaller example:

ntmax = 10;
Exp[V0 + ΔV*#/T] * Exp[V0 + ΔV*(# + T/ntmax)/T] &@
  Range[T/ntmax, T, T/ntmax]
  {13.4637, 20.0855, 29.9641, 44.7012, 66.6863, 99.4843, 148.413, 221.406, 330.3, 492.749}

If you want to save the result in a file, you can use DumpSave:

DumpSave["data.mx", data]

It can be read with Get.

  • $\begingroup$ great...very elegant approach to solve problem. Thanks a lot. $\endgroup$
    – zenith
    Jun 3 '14 at 16:32

A basic solution, which prints one iteration per line, is

(* create a temporary directory and move to it *)
outfile = "results.txt";
(* "touch" outfile *)
 (* append i to outfile *)
 PutAppend[i, outfile],
 {i, 5}]







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.