I have a Manipulate whose controls are generated based on a parameter in a containing Manipulate:
Manipulate[
With[{f = Table[c[i], {i, n}],
controls = Sequence @@ Table[{{c[i], 0}, -1, 1}, {i, n}]},
Manipulate[f, controls,
Button["Random", Do[c[i] = RandomReal[{-1, 1}], {i, n}]]]], {n, {3, 4, 5}}]
When I hit the "Random" button, to assign a random set of values to the parameters in the inner manipulate, chaos ensues, e.g.: "Manipulate argument {{-0.975768,0},-1,1} does not have the correct form for a variable specification."
If I change the above slightly, for example:
Manipulate[
With[{f = Table[c[i], {i, n}],
controls = Sequence @@ Table[{{c[i], 0}, -1, 1}, {i, n}]},
Manipulate[f, controls,
Button["Random", c[1] = RandomReal[{-1, 1}]]]], {n, {3, 4, 5}}]
I don't get similar errors (and c[1] gets randomly assigned a value as expected). I can't wrap my head around what's going on... what's the relevant difference between these two examples? How can I assign to a parameter as in the second example but using a loop as in the first?
Edit: Turns out I also have additional controls beyond the c[i]
, whose position should be maintained through the randomization of the c[i]
. For example, using belisarius' technique resets r
here:
Manipulate[
With[{f = Table[c[i], {i, n}],
controls = Sequence @@ Table[{{c[i], s[i]}, -1, 1}, {i, n}]},
Manipulate[Append[f, r], controls, {{r, 0}, -1, 1},
Button["Random", Do[s[i] = RandomReal[{-1, 1}], {i, n}]]]],
{n, {3, 4, 5}}, Initialization -> {s@_ := 0}]
Assigning directly to e.g. c[1]
as in the second example above does what I want:
Manipulate[
With[{f = Table[c[i], {i, n}],
controls = Sequence @@ Table[{{c[i], 0}, -1, 1}, {i, n}]},
Manipulate[Append[f, r], controls, {{r, 0}, -1, 1},
Button["Random", Do[c[1] = RandomReal[{-1, 1}], {i, n}]]]], {n, {3, 4, 5}}]
What's different about assigning directly to c[1]
as opposed to assigning to c[i]
with i == 1
?
Comments
Post a Comment