3
$\begingroup$

I want to use a generic function to generate controls in manipulate, which looks like this:

lincontrol[variable_, default_, min_, max_] :=
 {{{variable, default}, min, max}, 
  Panel[ToString[variable ] <> "=" Dynamic[variable]]}

so that I can use it inside a module like this:

    Manipulate[Module[{spectrum},
  spectrum = y^2 + a*x + b;
  Plot[spectrum, {x, -10, 10}]],
 Flatten[{lincontrol[#, 1, 0, 10] &/@ {y,a,b}}, 2]]

The idea is that lincontrol would generate a controller and display panel for y,a,b. The problem is that lincontrol is returning a list, whereas Manipulate just wants to take a series of arguments. I've used Flatten to get rid of the extra list layers, but I can't remove the 'outer' list. Any ideas?

$\endgroup$
  • 2
    $\begingroup$ Try Evaluate[Sequence @@ Flatten[{lincontrol[#, 1, 0, 10] & /@ {y, a, b}}, 2]] $\endgroup$ – Kuba Feb 12 '16 at 14:17
  • $\begingroup$ Something is wrong with spectrum = (x=y^2 +a*b;. You have no closing parenthesis. Maybe you mean x*y^2+a*b? $\endgroup$ – Jack LaVigne Feb 12 '16 at 15:19
3
$\begingroup$

It is not clear to me what you are trying to do; however, this is how to use a variable number of controls.

lincontrol[variable_, default_, min_, max_] :=
 {{variable, default}, min, max,
  Appearance -> "Labeled"}

Manipulate[
 Module[
  {spectrum},
  spectrum = y^2 + a*x + b;
  Plot[spectrum, {x, -10, 10},
   PlotRange -> {-100, 210}]],
 Evaluate[Sequence @@ (
    lincontrol[#, 1, 0, 10] & /@
     {y, a, b})]]

enter image description here

$\endgroup$
2
$\begingroup$

It seems to me that you want to show the current value for each control variable close to the control, and you constructed a Panel object to show that, which didn't seem to work.

I wonder if this alternative solution would work for you instead:

Clear[lincontrol]

lincontrol[variable_, default_, min_, max_] := 
    {{variable, default, ToString[variable] <> " ="}, min, max, Appearance -> "Labeled"}

Manipulate[
  Module[{spectrum},
    spectrum = x*y^2 + a*b;
    Plot[spectrum, {x, -10, 10}]
  ],
  Evaluate[Sequence @@ (lincontrol[#, 1, 0, 10] & /@ {y, a, b})]
]

Mathematica graphics

You could of course achieve the same results from within Manipulate as well, but your lincontrol function saves you some typing.

Notice also that I modified your definition of spectrum arbitrarily so it would be correct syntactically.

$\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.