6
$\begingroup$

Possible Duplicates:
Intermediate display similar to PrintTemporary
Estimating progress on plots
How to create a progress bar?
How to determine how much of a table is generated?

I'm trying to print an output from every iteration of a Do[] loop, but without it continuing down the notebook. I want the Print[] output to be overwritten in the same cell with the new value. Here is a MWE of what I'm trying, but I can't seem to get the expected behavior.

Do[
  If[i==1,
    Print["Output = ", Dynamic[i]]
  ]
,{i,10}
]

So in other words, on the first iteration, I want to see "Output = 1", and then every subsequent iteration I want to see the 1 change to a 2, 3, 4, etc. in the same cell.

$\endgroup$
4

3 Answers 3

5
$\begingroup$

You could use Monitor like :

Monitor[Do[Pause[3], {i, 1, 10^3}], i]
$\endgroup$
4
$\begingroup$

There are many, many possibilities to achieve something like that. If you want to use Dynamic, you could set a global value which you observe

Dynamic[globalWatch]

Do[
 If[OddQ[i], globalWatch = i];
 Pause[.1];
 ComplicatedCalculation[i],
 {i, 100}
 ]

I always suspect, whether Do is really needed, but sometimes when I for instance iterate over a list of files I want to process, I use it too. In such a situation, I use PrintTemporary as suggested by PlatoManiac to see the progress. PrintTemporary is nice because you can print all information you like and no matter it fills the screen, it gets erased after the computation is done.

$\endgroup$
4
$\begingroup$

Here is one way,

Print["Output = ", Dynamic[j]];
Do[j = i; Pause[0.1];, {i, 10}]
$\endgroup$

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