Define control as variable in Mathematica

后端 未结 2 1392
轻奢々
轻奢々 2021-02-09 08:13

When I use Manipulate I can do:

Manipulate[x, {u, 1, 10}]

In reality my controls are many and complicated, so I would prefer to take their defi

2条回答
  •  悲&欢浪女
    2021-02-09 08:58

    Manipulate has the HoldAll attribute. You can force control to evaluate and everything works ok

    control = {u, 1, 10};
    Manipulate[x[u], Evaluate[control]]
    

    The problem with this is that the variable u is not properly localised, so if you have already set, e.g., u=1 somewhere, then the Manipulate will return an error.

    It might be better if you use appropriate scoping constructs such as With or DynamicModule depending on exactly what you're trying to do.

    This is maybe overkill, but it ensures that u is local and moves control outside of the manipulate:

    DynamicModule[{u}, With[{control = {u, 1, 10}}, Manipulate[x[u], control]]]
    

提交回复
热议问题