3
$\begingroup$

Consider the following code, which give a Manipulate object that returns the "total" of a certain variable var 1

Manipulate[Total@x, Dynamic@Grid[{
    {"Number", 
     Slider[Dynamic@nv, {1, 10, 1}, Appearance -> "Labeled"]},
    {"var 1", Slider[Dynamic@x, {0, 100, 1}, Appearance -> "Labeled"]}
    }], ControlPlacement -> Top]

enter image description here

Now, imagine I want to define a list of variables var 1, var 2, ..., var 10 that depends on my choice for the first slider nv (number of variables), upon which I want to define a function (say Total, for example). I naively tried the following

Manipulate[Total@xl,
 Dynamic@Grid[{
    {"Number", 
     Slider[Dynamic@nv, {1, 10, 1}, Appearance -> "Labeled"]},
    Sequence @@
     Table[{StringForm["var ``", j], 
       Slider[Dynamic@(xl[[j]]), {0, 100, 1}, 
        Appearance -> "Labeled"]}, {j, nv}]
    }], ControlPlacement -> Top]

which dynamically updates the number of sliders, but returns the following error

enter image description here

It seems the variable list xl enters in conflict with Dynamic, but I think there's more to it that I don't understand. I also tried playing around with Symbol and creating something like Table[Symbol["$x" <> ToString@i], {i, 10}] to use as my variable list, but with no success. Indexed variables got me a bit closer, also by excluding j from Dynamic, but still not working:

Manipulate[Sum[xl[j], {j, nv}],
 Dynamic@Grid[{
    {"Number", 
     Slider[Dynamic@nv, {1, 10, 1}, Appearance -> "Labeled"]},
    Sequence @@
     Table[{StringForm["var ``", j], 
       Slider[Dynamic[xl][j], {0, 100, 1}, 
        Appearance -> "Labeled"]}, {j, nv}]
    }], ControlPlacement -> Top]

enter image description here

Any ideas?

$\endgroup$

2 Answers 2

4
$\begingroup$

What we have here is a logical situation in which an inner UI is to be generated by the value of an outer variable. So I would prefer to build the inner and outer components separately.

ClearAll[constructInnerUI, constructOuterUI];
constructInnerUI[nv_Integer] := nv // RightComposition[
    Range,
    Map[
        ToString /* OperatorApplied[StringJoin, 2]["x"] /* Symbol /*
        List /* Append[Range @ 100]
    ],
    Through @* {OperatorApplied[Part, {3, 1, 2}][All, 1] /* Total, Splice},
    OperatorApplied[Join][{Paneled -> False, FrameMargins -> None}],
    Apply[Manipulate]
];

constructOuterUI[] := Manipulate[
    constructInnerUI[nv],
    {nv, Range @ 100},
    Paneled -> False, FrameMargins -> None
];

constructOuterUI[]

Hopefully this will give you some ideas.

$\endgroup$
2
$\begingroup$

In the meantime, I found that using With might help. I just need to manually set the initial values for xl

Do[xl[j] = 1, {j, 10}];
Manipulate[Sum[xl[j], {j, nv}], Dynamic@Grid[{
    {"Number", 
     Slider[Dynamic@nv, {1, 10, 1}, Appearance -> "Labeled"]},
    Sequence @@ 
     Table[With[{j = j}, {StringForm["var ``", j], 
        Slider[Dynamic[xl[j]], {0, 100, 1}, 
         Appearance -> "Labeled"]}], {j, nv}]}],
 ControlPlacement -> Top]

Please let me know if there is a better or alternative way of doing this.

$\endgroup$
0

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.