# Assigning to the “local” version of a symbol

Compare

Manipulate[c = 1; c, {c, 0, 1}]


with

Manipulate[Do[c[i] = 1, {i, 1}]; c, {c, 0, 1}]


The first case sets the local c (the one displayed by Manipulate) to be 1. The second case sets the global value of c to be 1. Why do these two examples behave differently?

The problem I'm trying to solve here is assigning to a parameterized bunch of variables (more than the one in the examples above) inside a Manipulate construct. For context, this started out over here, but I've peeled it out as a separate question because this formulation seems more direct and more general.

Let's use WReach's method to examine Manipulate:

x = "global"; f[] := x

Manipulate[{x, f[], Hold[x]}, {x, {"local"}}]

{"local", "global", Hold[FEx$$69]}  This is akin to the output of Module. (See the WReach's post.) The value of x is not temporarily changed as with Block, nor are explicit x expressions directly replaced with the local value of x (i.e. "local") as in the case of With. Instead explicit appearances (and only explicit appearances) of x are first replaced with a localized equivalent, e.g. FEx$$69.

Now let's consider something closer to your example:

f[] := c

Manipulate[{c[i], c, f[]}, {c, 0, 1}]

{c[i], 0.524, c}


Note that only the explicit c has been changed; c[i] is not changed and neither is the global value of c as called by f[].

Again with Hold:

Manipulate[Hold[c[i], c, f[]], {c, 0, 1}]

Hold[c[i], FE\$5$$129, f[]]  From this point no special transformation of c[_] will occur; only the value of FE5$$129 will vary. Therefore if you change c[i] to c through Do and assign it a value it will be a global assignment.

If you describe what you wish to accomplish I can probably suggest a viable approach.

• Thanks! That clarifies a few things... so basically manipulate does the replacement of control variables once when it is initially evaluated, and there's no way to access the control variables indirectly? The thing I'm trying to accomplish is best described here. The answer provided there didn't quite solve my problem. I'll accept your answer here. I'd greatly appreciate it if you could take a look at my other question! – Dan Oct 25 '14 at 1:35

Your first example code actually is wrong, logically speaking. You are not supposed to make assignments to control variables (unless control type is set to None).

The control variable is supposed to be modified only using the controls (sliders, buttons etc...), and read only in the Manipulate expression. Otherwise, you can get into an infinite loop. When you change x yourself, Manipulate will then want to update its expression, then you change x again during this refresh (to new value), which causes Manipulate to want to refresh the expression again, ending in an infinite loop.

For your main question: c[i] is not the same as c as far as Manipulate symbol tracking is concerned. Even when i can become 1 at some later time. So when you write c[i]=1, and i happened to be 1, then it created new global symbol c during the evaluation. The Manipulate control variable c is not the same as c[i]. It has to be literally the same symbol, at the time the code is first read.

Your code was basically as if you typed

Manipulate[
Do[ z = 1, {i, 1} ];
c,
{c, 0, 1}
]


ps. I never seen indexed symbols used as control variables before actually, and I would not think it is a good idea for other things. Why not make a Table to store your data and make a normal symbol as control variable?

• I'm aware that the first example isn't code you should ever write in a real program (I just wanted a very minimal example), however modifying control variables is perfectly legitimate under certain circumstances, such as when using code executed after a Button press (e.g. reset, randomize). Using indexed symbols as I did above can be seen for example in the documentation on Advanced Manipulate Functionality under "Nesting Manipulate". Similar scoping issues arise even in the non-indexed case. – Dan Oct 25 '14 at 1:05