# Function for generically controlling 'Manipulate' parameters

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?

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

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})]]


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})]
]


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.