0
$\begingroup$

We have a long Do loop for our program in which we have put a counter to see the stage of process.

counter=0;Do[

  a program; counter+=counter;
  Print[counter]
  ,{i,1,10^6}];

But we do not want to have a cell devoted to each counter when it is printed. If this happens, we will have 10^6 cells below each other which consume a huge space in a notebook of Mathematica. Actually we wish to have a temporary printing process. Of course, the running program takes a long time to be done and we do not need Pause[nSeconds] that is emphasized in the Mathematica documents. We just want to see temporarily the counter once the program finished in every iteration! We have seen this link and this link but they do not work for us efficiently.

$\endgroup$
9
  • $\begingroup$ Check Monitor, or skip Print and create Dynamic @ counter before running the loop. $\endgroup$ – Kuba May 31 '17 at 6:44
  • $\begingroup$ Do you mean: Monitor[Dynamic@j, Do[j = i + 1, {i, 1, 40}]]? $\endgroup$ – Unbelievable May 31 '17 at 6:49
  • $\begingroup$ It just print 41!!! $\endgroup$ – Unbelievable May 31 '17 at 6:50
  • 1
    $\begingroup$ See also: How to create a progress bar $\endgroup$ – Kuba May 31 '17 at 6:53
  • 1
    $\begingroup$ Did you look up Monitor in the documentation? It is all explained there. Don't guess at the syntax, look it up!! $\endgroup$ – Szabolcs May 31 '17 at 7:27
3
$\begingroup$

Check the Documentation page for Monitor. Here is a quick example:

program := Pause[.2]
Monitor[Do[program, {i, 1, 40}], i]

Of course, the running program takes a long time to be done and we do not need Pause[nSeconds] that is emphasized in the Mathematica documents.

Pause[nSeconds] is just a stub for a program that takes significant time to compute, nothing more. Replace it with a call to your actual program!


Related:

$\endgroup$
1
$\begingroup$

You can use a file to decide if you want the program to print variables or not. For instance, only if the file exists, the program print variables which you are interested in.

It's tested on macOS, it should work on Linux as well.

  1. Evaluate the cell
  2. Execute the shell command whenever you want to check the status.

In:

Mathematica

ClearAll[debug]
debugQ[] := FileExistsQ["debug.txt"]
debugQ[]
Do[(Which[debugQ[], Print@i]; i), {i, 1, 10^8}]

Shell

echo 1 > debug.txt;rm debug.txt

Out:

enter image description here

$\endgroup$
12
  • $\begingroup$ Please explain what does the shell command echo 1 > debug.txt;rm debug.txt do exactly? Is it for Windows or for Linux? $\endgroup$ – Alexey Popkov Jun 2 '17 at 13:14
  • 2
    $\begingroup$ It might work on windows if you can use Bash on Windows. the shell command creates debug.txt and then delete it. During the short period of time, the program will print debug messages when the program detects that debug.txt exists. $\endgroup$ – UnchartedWorks Jun 2 '17 at 13:27
  • $\begingroup$ @UnchartedWorks Thanks, I assumed that it should be so but when run using cmd on Windows this command just creates the file "debug.txt" with contents "1 ;rm debug.txt". $\endgroup$ – Alexey Popkov Jun 2 '17 at 13:30
  • $\begingroup$ If the program takes long time to evaluate, it simply may not notice the quick creation-deletion procedure, which consequently won't have any effect. $\endgroup$ – Alexey Popkov Jun 2 '17 at 13:33
  • $\begingroup$ @AlexeyPopkov dir > debug.txt & del debug.txt $\endgroup$ – UnchartedWorks Jun 2 '17 at 13:34

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.