Prev: Functions
Next: auto-save in Mathematica
From: Dominic on 25 Feb 2010 17:33 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? Thanks guys, Dominic rtable = {0, 1, 2, 3, 4}; t1 = 0; t2 = 0; t3 = 0; t4 = 0; myf4[t1_, t2_, t3_, t4_] := {Line[{{Re[rtable[[1]]*Exp[I*t1]], Im[rtable[[1]]*Exp[I*t1]]}, {Re[rtable[[2]]*Exp[I*t1]], Im[rtable[[2]]*Exp[I*t1]]}}], Line[{{Re[rtable[[2]]*Exp[I*t2]], Im[rtable[[2]]*Exp[I*t2]]}, {Re[rtable[[3]]*Exp[I*t2]], Im[rtable[[3]]*Exp[I*t2]]}}], Line[{{Re[rtable[[3]]*Exp[I*t3]], Im[rtable[[3]]*Exp[I*t3]]}, {Re[rtable[[4]]*Exp[I*t3]], Im[rtable[[4]]*Exp[I*t3]]}}], Line[{{Re[rtable[[4]]*Exp[I*t4]], Im[rtable[[4]]*Exp[I*t4]]}, {Re[rtable[[5]]*Exp[I*t4]], Im[rtable[[5]]*Exp[I*t4]]}}]}; mycircles = Table[Circle[{0, 0}, rtable[[n]]], {n, 2, Length[rtable]}]; Manipulate[Show[Graphics[ {myf4[t1, t2, t3, t4], mycircles}], Axes -> True, PlotRange -> {{-5, 5}, {-5, 5}}], {t1, 0, 2*Pi}, {t2, 0, 2*Pi}, {t3, 0, 2*Pi}, {t4, 0, 2*Pi}, LocalizeVariables -> False]
From: Simon on 26 Feb 2010 04:05 Theres an example in tutorial/AdvancedDynamicFunctionality (section Nesting Dynamic) that has a variable number of controls. Maybe you could use something similar... On Feb 26, 6:33 am, "Dominic" <miliot...(a)rtconline.com> wrote: > 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? > > Thanks guys, > > Dominic > > rtable = {0, 1, 2, 3, 4}; > t1 = 0; > t2 = 0; > t3 = 0; > t4 = 0; > > myf4[t1_, t2_, t3_, t4_] := > {Line[{{Re[rtable[[1]]*Exp[I*t1]], > Im[rtable[[1]]*Exp[I*t1]]}, > {Re[rtable[[2]]*Exp[I*t1]], > Im[rtable[[2]]*Exp[I*t1]]}}], > Line[{{Re[rtable[[2]]*Exp[I*t2]], > Im[rtable[[2]]*Exp[I*t2]]}, > {Re[rtable[[3]]*Exp[I*t2]], > Im[rtable[[3]]*Exp[I*t2]]}}], > Line[{{Re[rtable[[3]]*Exp[I*t3]], > Im[rtable[[3]]*Exp[I*t3]]}, > {Re[rtable[[4]]*Exp[I*t3]], > Im[rtable[[4]]*Exp[I*t3]]}}], > Line[{{Re[rtable[[4]]*Exp[I*t4]], > Im[rtable[[4]]*Exp[I*t4]]}, > {Re[rtable[[5]]*Exp[I*t4]], > Im[rtable[[5]]*Exp[I*t4]]}}]}; > > mycircles = Table[Circle[{0, 0}, > rtable[[n]]], {n, 2, > Length[rtable]}]; > > Manipulate[Show[Graphics[ > {myf4[t1, t2, t3, t4], mycircles}], > Axes -> True, PlotRange -> > {{-5, 5}, {-5, 5}}], {t1, 0, 2*Pi}, > {t2, 0, 2*Pi}, {t3, 0, 2*Pi}, > {t4, 0, 2*Pi}, LocalizeVariables -> > False]
From: dh on 2 Mar 2010 03:36 Hello Dominic, here is a clumsy solution. Manipulate seems to be a very obstinate command, it does not listen to all the Hold typoe commands, neither With was working. So as a last resort, I got the big axe and made it a string. Sorry not very elegant, but a work around: Assume that rtable contains the radii of the different circles, n is the number of circles: rtable = {1, 2, 3, 4}; circles[n_] := Table[Circle[{0, 0}, r], {r, n}] sliders[n_] := Table[{Symbol["t" <> ToString[i]], 0, 2 Pi}, {i, n}] lines[n_] := Table[t = Symbol["t" <> ToString[i]]; Line[{If[i == 1, 0, rtable[[i - 1]]] {Cos[t], Sin[t]}, rtable[[i]] {Cos[t], Sin[t]}}], {i, 1, n}] ToExpression@ StringReplace[ "Manipulate[Show[Graphics[{circles,lines}],Axes\[Rule]True,\ PlotRange\[Rule]{{-5,5},{-5,5}}],sliders]", {"sliders" -> StringTake[ToString(a)sliders[4], {2, -2}], "circles" -> ToString[circles [4]], "lines" -> ToString[lines[4]]}] Daniel On 25.02.2010 23:33, Dominic wrote: > 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? > > Thanks guys, > > Dominic > > > rtable = {0, 1, 2, 3, 4}; > t1 = 0; > t2 = 0; > t3 = 0; > t4 = 0; > > > myf4[t1_, t2_, t3_, t4_] := > {Line[{{Re[rtable[[1]]*Exp[I*t1]], > Im[rtable[[1]]*Exp[I*t1]]}, > {Re[rtable[[2]]*Exp[I*t1]], > Im[rtable[[2]]*Exp[I*t1]]}}], > Line[{{Re[rtable[[2]]*Exp[I*t2]], > Im[rtable[[2]]*Exp[I*t2]]}, > {Re[rtable[[3]]*Exp[I*t2]], > Im[rtable[[3]]*Exp[I*t2]]}}], > Line[{{Re[rtable[[3]]*Exp[I*t3]], > Im[rtable[[3]]*Exp[I*t3]]}, > {Re[rtable[[4]]*Exp[I*t3]], > Im[rtable[[4]]*Exp[I*t3]]}}], > Line[{{Re[rtable[[4]]*Exp[I*t4]], > Im[rtable[[4]]*Exp[I*t4]]}, > {Re[rtable[[5]]*Exp[I*t4]], > Im[rtable[[5]]*Exp[I*t4]]}}]}; > > > mycircles = Table[Circle[{0, 0}, > rtable[[n]]], {n, 2, > Length[rtable]}]; > > > Manipulate[Show[Graphics[ > {myf4[t1, t2, t3, t4], mycircles}], > Axes -> True, PlotRange -> > {{-5, 5}, {-5, 5}}], {t1, 0, 2*Pi}, > {t2, 0, 2*Pi}, {t3, 0, 2*Pi}, > {t4, 0, 2*Pi}, LocalizeVariables -> > False] > > > -- Daniel Huber Metrohm Ltd. Oberdorfstr. 68 CH-9100 Herisau Tel. +41 71 353 8585, Fax +41 71 353 8907 E-Mail:<mailto:dh(a)metrohm.com> Internet:<http://www.metrohm.com>
From: Albert Retey on 2 Mar 2010 07:58 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}]
From: Christoph Lhotka on 3 Mar 2010 05:51
hi, although once somebody from WRI told me not to do so (when writing a demonstration), I like the idea of nesting Manipulate's : Manipulate[ Manipulate[{x, y, z}, Evaluate[Sequence @@ Take[{{x, 0, 1}, {y, 0, 1}, {z, 0, 1}}, len]]], {len, {1, 2, 3}}] will construct a Manipulate object with variable numbers of controllers. Christoph Dominic wrote: > Hi, > > Can I set up a Manipulate with a variable number of controls? The code > |