1
$\begingroup$

I would like to use a local variable within Dynamic, but that may not work, and the front end will show the local variable in red to give a warning. However, if I define a global variable to equal the local one, then I can use the global one inside Dynamic. Will this always work, or is it just a way to hide a problem that may occur? For example, this works:

Manipulate[
 Block[{data = RandomReal[{0, 1}, {2, 2}]}, dataG = data; 
  Dynamic@ArrayPlot[dataG^p]], {p, 0.1, 10}, 
 SynchronousUpdating -> False]

(In the present example, since the only effect on the plot of changing p is to change the color, one could also use ColorFunction.)

I stripped away many details from the application I have in mind. Rather than a simple data, I have a complicated calculation to do that depends on other control variables of the Manipulate. And that is the other reason for using Dynamic and why I want the local variable to be defined within Manipulate.

If this will not always work, is there a convenient way to introduce global variables so that they do not conflict with other global variables in the same session and without having to remember or check what has been used already?

$\endgroup$
7
  • $\begingroup$ Does the further scoping have to be inside Manipulate? Otherwise you could wrap the Manipulate[ArrayPlot[...], ...] in a scoping construct, eg Module, DynamicModule, or With (but not Block): Module[{data = RandomReal[{0, 1}, {2, 2}]}, Manipulate[ArrayPlot[data^p], {p, 0.1, 10}, SynchronousUpdating -> False]]. You won’t need Dynamic inside this way. $\endgroup$ – MarcoB Jan 9 at 19:44
  • $\begingroup$ @MarcoB Yes, it does have to be inside. I stripped away many details from the application I have in mind. Rather than a simple data, I have a complicated calculation to do that depends on other control variables of the Manipulate. And that is the other reason for using Dynamic. $\endgroup$ – Russ Lyons Jan 9 at 19:54
  • 1
    $\begingroup$ I see. Please add that constraint and context to the question. $\endgroup$ – MarcoB Jan 9 at 19:57
  • $\begingroup$ I'd use With on the example: Manipulate[With[{data = RandomReal[{0, 1}, {4, 4}]}, Dynamic@ArrayPlot[data^p]], {p, 0.1, 10}, SynchronousUpdating -> False] $\endgroup$ – Michael E2 Jan 9 at 21:06
  • $\begingroup$ @MichaelE2 I don't think I can use With because I need to to some calculations before I obtain the value of data, and these depend on other control variables. $\endgroup$ – Russ Lyons Jan 9 at 21:19
4
$\begingroup$

Two alternatives:

Using With (use multiple ones if necessary):

Manipulate[
 With[
  {data = RandomReal[{0, 1}, {4, 4}]},   
  Dynamic@ArrayPlot[data^p]],
 {p, 0.1, 10}, 
 SynchronousUpdating -> False]

Use a local variable with no control:

Manipulate[ 
 data = RandomReal[{0, 1}, {4, 4}]; 
 Dynamic@ArrayPlot[data^p],
 {p, 0.1, 10},
 {data, None}, 
 SynchronousUpdating -> False] 

One advantage of With is that it does not create a tracked symbol, which sometimes simplifies the Dynamic behavior of the Manipulate. Two disadvantages of With are that the variable is constant, and many nested With are inelegant.

$\endgroup$
2
  • 3
    $\begingroup$ The first does not have a footprint on the notebook size out of the box, the second one requires UnsavedVariables. $\endgroup$ – Kuba Jan 10 at 6:26
  • $\begingroup$ @Kuba Thanks. I thought I was going to say that, except about UnsavedVariables. That option had eluded my attention, maybe because I stopped contributing to the demonstrations project around the time V8 came out. $\endgroup$ – Michael E2 Jan 10 at 14:02

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.