Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

Do[
 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.

share|improve this question
1  
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. –  bill s Jun 2 at 16:53
2  
Put is also useful. Your question is very broad though. A more specific question (with examples and preferrably code) will get better answers. –  Yves Klett Jun 2 at 17:31
    
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. –  Nasser Jun 2 at 18:52
2  
Please use mathematica input form.. For this simple example simply replacing Do with Table will generate a list of sucessive f values. –  george2079 Jun 2 at 20:01
1  
@Mehdi , yours is an example where the loop execution time is so long that the file write time is not significant. –  george2079 Jun 3 at 11:06

3 Answers 3

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.

share|improve this answer
    
great...very elegant approach to solve problem. Thanks a lot. –  zenith Jun 3 at 16:32

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

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

1

2

3

4

5

share|improve this answer
    
thanks for the guidance –  zenith Jun 3 at 16:34

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 *)
Do[
F[i](* calculates what you like as a function of i *)
Write[file, F[i]]; (* writes F[i] in ith line of the file*)
,{i,1,n}]
Close[file];

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

Write[file,f];

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.

share|improve this answer
    
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. –  zenith Jun 2 at 22:26
    
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). –  Mahdi Jun 2 at 22:38
1  
this helped to save my data and retrieve while using do loop. Many thanks –  zenith Jun 3 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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