0
$\begingroup$

My example function

progressFunction[time_] := Module[{t}, Monitor[For[t = 0, t <= time, t++, Pause[1]], ProgressIndicator[t, {0, time}]]]

does exactly what I intend when I call it directly:

progressFunction[5]

(displays progress bar increasing from 0% to 100% over 5 seconds).

But when this function is called from within Manipulate:

Manipulate[progressFunction[time], {time}]

the Monitor cell displays:

Error

Interestingly, when I copy/paste the above text into a new cell, it displays correctly in the new cell.

Any idea what's going on and how to fix this? This is pretty annoying because my real Manipulate expression calls functions that take a long time to complete, so I have implemented Monitor on all of them, but they all become useless under Manipulate.

$\endgroup$

1 Answer 1

1
$\begingroup$

I'm not sure exactly how you want the interface. Here's way to monitor the progress but without Monitor:

ClearAll[progressFunction];
progressFunction[time_, t_] := Module[{},
   If[time > 0,
    For[t = 0, t <= time, t++, Pause[1]]
    ];
   t = 0;
   ];

Manipulate[
 progressFunction[time, Unevaluated@progress];
 {time, progress},
 {{time, 0}, InputField},
 {{progress, progress}, 0, time,
  Replace[Hold[##], 
    Hold[Dynamic[p_], i__] :> Dynamic@ProgressIndicator[p, i]] &},
 TrackedSymbols :> {time}]

Here's a way using Monitor but it's not as good since it evaluates twice. I cannot be sure whether that would happen in your actual use case, though.

ClearAll[progressFunction];
progressFunction[time_, t_] :=
  Monitor[
   For[t = 0, t <= time, t++, Pause[1]],
   ProgressIndicator[t, {0, time}]
   ];

Manipulate[
 progressFunction[time, Unevaluated@timer];
 {time, timer},
 {time, InputField}, {{timer, timer}, None},
 TrackedSymbols :> {time}]
$\endgroup$
4
  • $\begingroup$ Unfortunately, my true analog of progressFunction involves several other calls to several other functions, many of which use Monitor. Some monitor variables in For loops, while others monitor variables in FindMinimum. They also all use the trick f[x_]:=f[x]=..., which gets ruined when the extra Unevaluated@variable parameters are used. $\endgroup$
    – WillG
    Commented May 10, 2020 at 2:23
  • $\begingroup$ In short, I tried to implement these solutions in my code but they didn't work, and I think making them work would require some pretty drastic changes to my code structure. $\endgroup$
    – WillG
    Commented May 10, 2020 at 2:24
  • 2
    $\begingroup$ @WillG I guess the problem is that your question didn't have some important details. $\endgroup$
    – Michael E2
    Commented May 10, 2020 at 2:35
  • $\begingroup$ I just thought there would be a simple fix that wouldn't require significantly changing my existing code. I just need the Monitor cells to display the way they're supposed to, after all. $\endgroup$
    – WillG
    Commented May 10, 2020 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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