1
$\begingroup$

I have a list, and length of this list can be changed sometimes. I want to define manipulation parameters inside "Manipulate" according to length of the list.

For example:

list={1,2,3,4};
Manipulate[
Row[{
Which[
  Length[list] == 1, Control@{{n1, 1, "a"}, 0, 9, 1},
  Length[list] == 2, (Control@{{n1, 1, "a"}, 0, 9, 1},
                      Control@{{n2, 5, "b"}, 1, 2, 0.1})
  ]
}]
]
$\endgroup$
  • $\begingroup$ Hi Valinor, welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context. Thanks for your minimal working example and code in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ – rhermans Jul 11 at 13:57
0
$\begingroup$
list = {1, 2, 3, 4};

Attributes[Manipulate]

(* {HoldAll, Protected, ReadProtected} *)

There are multiple problems with your code. Since the Attributes for Manipulate include HoldAll, you must Evaluate the Which for the controls to be seen as such. At least one of the tests of the Which must be satisfied. The default for the controls must fall within the ranges specified for the controls. The Manipulate must include an expression prior to the control(s). Parentheses cannot be used to hold a Sequence.

Manipulate[
 list,
 Row[{
   Evaluate@
    Which[
     Length[list] == 1, Control@{{n1, 1, "a"}, 0, 9, 1,
       Appearance -> "Labeled"},
     Length[list] > 1, Sequence @@ {
       Control@{{n1, 1, "a"}, 0, 9, 1,
         Appearance -> "Labeled"},
       Control@{{n2, 5, "b"}, 1, 5, 0.1,
         Appearance -> "Labeled"}}]}]]

enter image description here

You could also use If

Manipulate[
 list,
 Row[{
   Control@{{n1, 1, "a"}, 0, 9, 1,
     Appearance -> "Labeled"},
   Evaluate@If[Length[list] > 1,
     Control@{{n2, 5, "b"}, 1, 5, 0.1,
       Appearance -> "Labeled"},
     Nothing]}]]

enter image description here

$\endgroup$
0
$\begingroup$

Thanks Bob Hanion. The answer was very helpful. But I have still some error. I am writing may code part which interested with MAnipulate. Thanks for the answers.

In[12]:= symbol = {"a", "b", "c"}

Out[12]= {"a", "b", "c"}

In[13]:= value = {1, 2, 3}

Out[13]= {1, 2, 3}

In[19]:= Manipulate[value,

 Row[{
   Text[Style["-------- FIT MANIPULATION --------", FontSize -> 30, 
     Bold, Black]], "\n\n",
   Evaluate@Which[
     Length[value] == 1, 
     Control@{{n1, value[[1]], symbol[[1]]}, -3, 1, 0.1, 
       Appearance -> "Labeled"},
     Length[value] == 2, Sequence @@ {

       Control@{{n1, value[[1]], symbol[[1]]}, -3, 1, 0.1, 
         Appearance -> "Labeled"},

       Control@{{n2, value[[2]], symbol[[2]]}, -7, 1, 0.1, 
         Appearance -> "Labeled"}  },
     Length[value] == 3, Sequence @@ {

       Control@{{n1, value[[1]], symbol[[1]]}, -3, 1, 0.1, 
         Appearance -> "Labeled"},

       Control@{{n2, value[[2]], symbol[[2]]}, -7, 1, 0.1, 
         Appearance -> "Labeled"},  

       Control@{{n3, value[[3]], symbol[[3]]}, -3, 1, 0.1, 
         Appearance -> "Labeled"}   },
     Length[value] == 4, Sequence @@ {

       Control@{{n1, value[[1]], symbol[[1]]}, -3, 1, 0.1, 
         Appearance -> "Labeled"},

       Control@{{n2, value[[2]], symbol[[2]]}, -7, 1, 0.1, 
         Appearance -> "Labeled"},  

       Control@{{n3, value[[3]], symbol[[3]]}, -3, 1, 0.1, 
         Appearance -> "Labeled"},

       Control@{{n4, value[[4]], symbol[[4]]}, -3, 1, 0.1, 
         Appearance -> "Labeled"}   }
                ](*End of Which*)

   }](*End of Row*)
 ](*End of Manipulate*)

enter image description here

$\endgroup$

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.