1
$\begingroup$

I have a list named data that contains lists of arbitrary size,

data={{0.5, 0.5}, {}, {0.5, 0.5, 0.5}, {}, {}, {}, {}, {}, {}, {}}

I now want to generate for each sub-list the appropriate number of sliders (for the empty sets none, though I believe they are easy to skip). However, I am not sure if this is even possible.

In my quest I stumbled upon this thread, Manipulate with a variable number of sliders, which allows me to generate sliders for one sub-list,

Dynamic[Grid[
Table[With[{i = i}, {Slider[Dynamic[data[[1]][[i]]]], 
 Dynamic[data[[1]][[i]]]}], {i, Length[data[[1]]]}]]] 

This works perfectly fine if I use a constant as an index for data, in this case 1.

However, if I want to extend this by using a for loop or a table to make that index variable, say

Table[Dynamic[Grid[
Table[With[{i = i}, {Slider[Dynamic[data[[k]][[i]]]], 
 Dynamic[data[[k]][[i]]]}], {i, Length[data[[k]]]}]]],{k,1,1}]

the k gets marked red and the code fails. Is it even possible to generate sliders this way? I imagine the reason that the code fails is that Dynamic requires the object under consideration to remain constant, but is there a way around this?

$\endgroup$
2
  • $\begingroup$ What does the numbers in the sublists represent? $\endgroup$
    – C. E.
    Commented Jun 16, 2014 at 22:02
  • $\begingroup$ Probabilities which exist for some variables but not for others. $\endgroup$ Commented Jun 16, 2014 at 22:04

1 Answer 1

4
$\begingroup$

May be something like this would work:

   data2 = Select[data, Length@# > 1 &];

Dynamic[Column@
  Flatten@Table[
    With[{i = i, k = k}, {Slider[Dynamic[data2[[k]][[i]]]], 
      Dynamic[data2[[k]][[i]]]}], {k, 1, Length@data2, 1}, {i, 1, 
     Length@data2[[k]]}]]

Second option: To work directly with data you can try this:

 data = {{0.5, 0.5}, {}, {0.5, 0.5, 0.5}, {}, {}, {}, {}, {}, {}, {}};
 index = Complement[Range[Length@data], Position[data, {}] // Flatten];

Dynamic[Column@
  Flatten@Table[
    With[{i = i, k = k}, {Slider[Dynamic[data[[k]][[i]]]], 
      Dynamic[data[[k]][[i]]]}], {k, index}, {i, 1, 
     Length@data[[k]]}]]
$\endgroup$
3
  • $\begingroup$ Is there a way for the sliders to change the values in the original data, not data2? Apart from that it is exactly what I am looking for. $\endgroup$ Commented Jun 16, 2014 at 22:34
  • $\begingroup$ @hpschrei I have added what you want. $\endgroup$ Commented Jun 17, 2014 at 2:12
  • $\begingroup$ Perfect, thank you so much! $\endgroup$ Commented Jun 17, 2014 at 6:01

Your Answer

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

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