I'm using a dynamic number of sliders and I want to access the current values of them. The values can be seen on the labels. However, I want them to be stored into an array. Below is what I have done so far:

  s = ConstantArray[0.5, n]; 
 Table[{"Slider "[i], 
   Slider[s[[i]], Appearance -> "Labeled"]}, {i, 1, n, 1}]]}], {n,
 2, 10, 1}]

Clearly, the initial values are stored in $s$ and I want the values to be updated dynamically.

  • 1
    $\begingroup$ ~~ Table[With[{i=i}, ...Slider[Dynamic[s[[i]] ],...]... See documentation of Slider. $\endgroup$ – Kuba May 20 '17 at 12:32
  • 1
    $\begingroup$ See A Good Trick to Know. $\endgroup$ – Michael E2 May 20 '17 at 13:19
  • $\begingroup$ @MichaelE2 Oh! I looked at that page earlier. But I just overlooked that specific portion. Thanks to both of you(@Kuba, @MichaelE2) $\endgroup$ – Majis May 20 '17 at 13:28

Perhaps not easy to find in the documentation, but answered clearly in the section called A Good Trick to Know in the tutorial, "An Introduction to Dynamic":

This can be done with /. or with the somewhat peculiar but convenient idiomatic form demonstrated here.

Table[With[{i = i}, Slider[Dynamic[data[[i]]]]], {i, 5}]

This output shows that Dynamic does in fact work perfectly with part extraction syntax, a very useful property.

  • $\begingroup$ on what way to do the same using /. ? $\endgroup$ – Dragutin Mar 26 '18 at 20:10
  • $\begingroup$ @Dragutin Something like Table[Slider[Dynamic[data[[i]]] /. HoldPattern[i] -> i], {i, 5}] $\endgroup$ – Michael E2 Mar 26 '18 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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