Re: Manipulate with variable number of controls
- To: mathgroup at smc.vnet.net
- Subject: [mg107903] Re: Manipulate with variable number of controls
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Tue, 2 Mar 2010 07:53:21 -0500 (EST)
- References: <hm6ts0$ggb$1@smc.vnet.net>
Am 25.02.2010 23:33, schrieb Dominic:
> Hi,
>
> Can I set up a Manipulate with a variable number of controls? The code
> below draws 4 circles and line segments between them at the angles given
> by the slider values. As you can see, I had to hard-code the function
> for the lines, and the number of controls given by the size of rtable.
> Is is possible to dynamically construct this Manipulate based on the
> size of rtable? I noticed if I try to build a control outside of
> Manipulate such as mycontrol1={"t1",0,2 PI}, and then use mycontrol1
> inside of Manipulate, it doesn't work. Also, I use
> LocalizeVariales->False since I then go on to use the values of the
> angles (t1,t2, ...) in the program.
>
> Can anybody help me to get this working for arbitrary-size rtable with
> not more than five entries, or is Manipulate not capable of dynamically
> allocating a variable number of controls?
I have seen there is another solution, but here is another version that
doesn't need converting to strings and back. The trick I am using is to
contruct the Manipulate arguments wrapped with Hold and only in the end
apply Manipulate. I also am using the fact that Manipulate accepts
variable definitions like t[1]. Unfortunately that is realized in a way
that makes it necessary to have explicit occurances of t[1],... appear
in the body of the Manipulate, so that is why everything needs to be a
little more complicated that one would wish...
lines[tvals_List] := Table[
Line[{
{Re[rtable[[i]]*Exp[I*tvals[[i]]]],
Im[rtable[[i]]*Exp[I*tvals[[i]]]]},
{Re[rtable[[i + 1]]*Exp[I*tvals[[i]]]],
Im[rtable[[i + 1]]*Exp[I*tvals[[i]]]]}
}],
{i, Length[tvals]}
]
circles[rtable_List] :=
Table[Circle[{0, 0}, rtable[[n]]], {n, 2, Length[rtable]}];
rtable = Range[0, 5];
Manipulate @@ Replace[With[{
controls = Hold @@ Table[
With[{ii = i},
Hold[{tval[ii], 0, Subscript["t", ii]}, 0, 2 Pi]],
{i, Length[rtable] - 1}
],
tvals = Table[tval[i], {i, 1, Length[rtable] - 1}]
},
Join[
Hold[Show[Graphics[{lines[tvals], circles[rtable]}]]],
controls,
Hold[Axes -> True, PlotRange -> {{-5, 5}, {-5, 5}}]
]
], Hold[a___] :> List[a], {1}]