在 Mathematica 中将控件定义为变量 [英] Define control as variable in Mathematica
问题描述
当我使用 Manipulate 时,我可以:
When I use Manipulate I can do:
Manipulate[x, {u, 1, 10}]
实际上我的控件很多而且很复杂,所以我更愿意采用它们的Manipulate 表达式之外的定义,如下所示:
In reality my controls are many and complicated, so I would prefer to take their definition out of the Manipulate expression, like that:
control = {u, 1, 10}
Manipulate[x, control]
但这确实会导致错误:
Manipulate argument control does not have the correct form for a \
variable specification.
为什么它不能那样工作?
Why doesn't it work that way?
推荐答案
Manipulate
有 HoldAll
属性.您可以强制 control
进行评估,一切正常
Manipulate
has the HoldAll
attribute. You can force control
to evaluate and everything works ok
control = {u, 1, 10};
Manipulate[x[u], Evaluate[control]]
问题在于变量 u
没有正确本地化,所以如果您已经在某处设置了例如 u=1
,那么 操作
会返回错误.
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.
如果您使用适当的作用域结构(例如 With
或 DynamicModule
),这可能会更好,具体取决于您要执行的操作.
It might be better if you use appropriate scoping constructs such as With
or DynamicModule
depending on exactly what you're trying to do.
这可能有点矫枉过正,但它确保 u
是本地的并将 control
移到操作之外:
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]]]
这篇关于在 Mathematica 中将控件定义为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!