1
$\begingroup$

I found myself reusing this code all the time to monitor variables during execution, so I'm trying to make it into a function:

pdr[expr_, symbols_List: {}, interval_: \[Infinity]] := 
  PrintTemporary[
   Dynamic[Refresh[expr, TrackedSymbols :> symbols, 
     UpdateInterval -> interval]]];

But then when I try to use it, as with something like

s={};
pdr[s,{s},1];

Do[
    AppendTo[s,n];
    Pause[1];
,{n,3}]

it doesn't seem to work. If I paste in the exact same PrintTemporary line that the function is calling in place of my pdr function, it works as expected. It seems like there's some quirk of Mathematica I'm overlooking here, so any help appreciated.

$\endgroup$
2
$\begingroup$

The argument {s} in pdr[s, {s}, 1] evaluates to {{}}. So the TrackedSymbols option of Refresh does not see a list of symbols.

One fix:

Clear[s];
pdr[s, {s}, 1];
s = {};

Do[AppendTo[s, n];
 Pause[1];, {n, 3}]

Another:

ClearAll[pdr];
SetAttributes[pdr, HoldAll];
pdr[expr_, symbols_List: {}, interval_: \[Infinity]] :=
  Block[symbols,
   PrintTemporary[
    Dynamic[Refresh[expr, TrackedSymbols :> symbols, 
      UpdateInterval -> interval]]]
   ];
$\endgroup$
  • $\begingroup$ Ah, perfect. Thanks! $\endgroup$ – Trev Aug 18 at 17:09

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.