1
$\begingroup$

I want to create a DynamicModule that has several variables that were automatically created. For example:

DynamicModule[{a1, a2, 
  a3}, {{Slider[Dynamic[a1]], Slider[Dynamic[a2]], 
   Slider[Dynamic[a3]]}, {Dynamic[a1], Dynamic[a2], Dynamic[a3]}}]

here there are three different sliders which all have different variables associated with it. Since I later want to adjust the number of sliders and variables dynamically, I basically want to automatize this such that I can create multiple sliders each with its own variable.

So I tried to build the variables, but this totally fails:

   DynamicModule[ToExpression@Table["a" <> ToString@x, {x, 1, 3}], 
 Slider[Dynamic[#]] & /@ Table["a" <> ToString@x, {x, 1, 3}], 
 Dynamic@Table[ToExpression["a" <> ToString@x], {x, 1, 3}]]

Can someone help me out here?

$\endgroup$

1 Answer 1

2
$\begingroup$

Update

Kuba and Holistic pointed out some issues with the original answer. This new version addresses those issues: To insert the built variable names in such a way that they're not affected by previous values, we need to use the three argument form of ToExpression to wrap the resulting expression in Inactive. After inserting them into DynamicModule, we can Activate the whole thing again.

With[
 {vars = Table[
    ToExpression["a" <> ToString@i, InputForm, Inactive], {i, 5}]},
 ReleaseHold@Activate@Hold@DynamicModule[
     vars,
     {Slider[Dynamic[#]] & /@ vars,
      Slider[Dynamic[#]] &@vars[[1]],
      Slider[Dynamic[Evaluate@vars[[1]]]],
      Dynamic /@ vars, 
      Dynamic@vars[[1]],
      Dynamic@a1}
     ]
 ]

The code above also demonstrates two ways how to solve the issue encountered by Holistic: Slider@Dynamic@vars[[1]] does not work as expected. The reason for this is once again a Hold... attribute (HoldFirst of Dynamic in this case). To get vars[[i]] to evaluate before getting wrapped by Dynamic, we can either call Evaluate or simply make a pure function and apply that to vars[[1]].

Old way

One way to achieve what you're after is to generate the list of variables before and insert them into DynamicModule using With:

With[
 {vars = Table[ToExpression["a" <> ToString@i], {i, 5}]},
 DynamicModule[
  vars,
  {
   Slider[Dynamic[#]] & /@ vars,
   Dynamic /@ vars
   }
  ]
 ]

The problem with your approach arises due to the following two properties of DynamicModule: It has attribute HoldAll and it localized the variables. Combined, this means that DynamicModule doesn't see the variables at the time of first evaluation, resulting in them not being localized.

(You could also try to simply put an Evaluate around the variable declaration to at least get valid symbols to localize, but you'll run into other issues depending on how you use your variables later.)

$\endgroup$
5
  • $\begingroup$ Ah ok, I see! Thank you for the explanation! $\endgroup$
    – holistic
    Aug 22, 2017 at 17:42
  • $\begingroup$ One quick follow-up questions: Can you explain why I am not able to access these values like this? With[{vars = Table[ToExpression["a" <> ToString@i], {i, 2}]}, DynamicModule[ vars, {Slider[Dynamic[vars[[1]]]], Dynamic[vars[[1]]], Dynamic[vars[[1]]]}]] $\endgroup$
    – holistic
    Aug 22, 2017 at 18:01
  • $\begingroup$ @Kuba: Ah ok! I think I found a way to make it work: With[{vars = Table[ToExpression["a" <> ToString@i], {i, 2}]}, DynamicModule[{blub = vars}, {Slider[Dynamic[blub[[1]]]], Slider[Dynamic[blub[[2]]]], Dynamic[blub[[1]]], Dynamic[blub[[2]]]}]] $\endgroup$
    – holistic
    Aug 22, 2017 at 19:24
  • $\begingroup$ @Kuba & holistic: Thanks for pointing out those issues. I have updated the answer to address them. $\endgroup$
    – Lukas Lang
    Aug 22, 2017 at 21:37
  • $\begingroup$ @Mathe172: Thanks for making an update! Works very well. $\endgroup$
    – holistic
    Aug 22, 2017 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.