Skip to main content

Manipulate with a variable number of sliders


I have a function that operates on a list of parameters of variable-length $n$. I would like to create a Manipulate[] that has $n$ sliders, one for each list element, each considered a separate parameter. The syntax Manipulate[expr,{u,...},{v,...},...]] does not lend itself to this, as it is geared toward a fixed number of parameters known in advance, and referenced by separate variable names.


I have started exploring preparing a list of arguments to Manipulate[] and then using Apply[], but this seems tricky and complicated.


Anyone come upon this conundrum before?



Answer



The Advanced Dynamic Functionality in Mathematica documentation has the following example that looks like what you need.



DynamicModule[{n = 5, data = Table[RandomReal[], {20}]},
Column[{
Slider[Dynamic[n], {1, 20, 1}],
Dynamic[Grid[Table[With[{i = i},
{Slider[Dynamic[data[[i]]]], Dynamic[data[[i]]]}], {i, n}]
]]}]]

nested sliders


It builds a list of controllers (Slider-s in this particular case) by using the fact that you can assign values to not just symbols but also to members of a list by doing data[[1]] = value. Which is exactly the thing that happens inside Dynamic[data[[i]]], as it is equivalent to:


Dynamic[data[[i]], (data[[i]] = #)&]


telling Mathematica to whenever the actual value of data[[i]] is changed, use the new value (#) to update the expression data[[i]].


Also from the Documentation Center, the last example in Manipulate: Neat Examples may be useful:


Manipulate[
ArrayPlot[
Take[data, h, w]], {{data, RandomInteger[{0, 1}, {10, 20}]},
ControlType -> None}, {{h, 5}, 1, 10, 1}, {{w, 5}, 1, 20, 1},
Dynamic[Panel[
Grid[Outer[Checkbox[Dynamic[data[[#1, #2]]], {0, 1}] &, Range[h],
Range[w]]]]]]


which gives


enter image description here


Comments