如何“保存定义”不使用 Manipulate 结构?
我正在制作一个 CDF 文档,其中将动态片段放置在文本中间。该文档应该可以在播放器中使用。
注册应该从分散在不同单元格周围的所有动态片段访问的全局值的最佳方法是什么?
例如,我想定义 g=9.8,并且我希望放置在不同单元格中的所有动态都可以访问该值。由于这应该对播放器有效,因此该值应该在 Mathematica 会话中持续存在。
我只能想到两种不同的方法:
- 用户打开文件时必须单击的操作按钮,启动笔记本周围使用的所有所需的 = 和 :=
- 创建一个空操作,并使用 SaveDefinitions-> ;确实如此,并且其变量未本地化到 Manipulate
两者似乎都太人为了。
使用任何其他仅在显示后设置定义的方法都不好,因为接受显示动态的人可能已经向下滚动,并通过了实现所有所需定义的动态。
那么,如何在不使用 Manipulate 结构的情况下“SaveDefintions”呢?
I'm doing a CDF document where I'm placing Dynamic pieces in the middle of the text. This document is supposed to work in the Player.
What is the best way to register glogal values that should be accessed from all the dynamic pieces that are scattered around different cells?
For instance, I want to define g=9.8, and I want this value to be accessed by all the dynamics that are placed in the different cells. Since this should work on the player, this value should persist across sessions of Mathematica.
I could only think of 2 different ways:
- an action button that has to be clicked by the user, when he opens the file, that launches all the needed = and := used around the notebook
- create an empty manipulate, with a SaveDefinitions->True, and whose variable are not localized to the Manipulate
Both seem too artificial.
Having any other method that only sets the definitions once it is displayed is not good, since the person that accepts to display the dynamics may have already scrolled down, and passed by the dynamic that implements all the needed definitions.
So, how to "SaveDefintions" without using the Manipulate structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种可能性是为文档中的每个
Dynamic
对象定义条件初始化
,并将初始化表达式放置在初始化单元格中(或放置在带有标签的其他单元格中,以便轻松识别它) )。例如,使用初始化单元:通过这种方式,您不需要在每个动态对象中存储初始化表达式,并且不会为每个对象重复计算这些表达式。
更新
看来 Notebook 的
NotebookDynamicExpression
选项正是您想要的。 John Fultz 写道:“
Dynamic
可以存储在前端选项CellDynamicExpression
、NotebookDynamicExpression
和FrontEndDynamicExpression
。这些Dynamic
不会显示,但会在显示它们所附加的单元格/笔记本/前端时更新。”所以解决方案是:
评估上述内容后尝试保存并再次打开笔记本。并检查
a
的定义。One possibility is to define conditional
Initialization
for everyDynamic
object in the document and place initialization expressions in an initialization cell (or in some other cell with a tag which allow easily identify it). For example, using initialization cell:In this way you need not to store initialization expressions in every
Dynamic
object and these expressions will not be evaluated repeatedly for every of them.Update
It seems that
NotebookDynamicExpression
option of Notebook is what you want. John Fultz wrote about it:"
Dynamic
s can be stored in the front end optionsCellDynamicExpression
,NotebookDynamicExpression
, andFrontEndDynamicExpression
. TheseDynamic
s are not displayed, but are updated when the cell/notebook/frontend to which they are attached is displayed."So the solution is:
Try to save and open again notebook after evaluating the above. And check definition for
a
.我没有像您一样在动态文档中尝试过此操作,但您可以尝试以下操作:
“使用初始化单元,您可以指定应首先评估笔记本的特定输入单元。这可确保您的代码以正确的顺序评估,例如在评估使用这些定义的单元格之前定义函数。”
http://reference.wolfram.com/mathematica/howto/WorkWithInitializationCells.html
I have not tried this in dynamics document like you have, but you can try this:
"Using initialization cells, you can specify that particular input cells of a notebook should be evaluated first. This ensures that your code is evaluated in the correct order, such as defining functions before evaluating cells that use those definitions."
http://reference.wolfram.com/mathematica/howto/WorkWithInitializationCells.html
您可以使用以下内容:
在第一次评估时
string
不存在,因此 StringQ 会导致 Dynamic 显示“”。此后,StringQ 的计算结果为 True,Dynamic 将显示string
中的文本。savetext
通过作为 DynamicModule 局部变量跨会话保存。string
可以直接更新,例如string = "new text"
但是,如果同时打开此构造的多个副本,则一个
string< /code> 变量将更新另一个。
看到有关如何隔离实例(无需重命名“字符串”)的建议将会很有趣。
You can use something along these lines:
On first evaluation
string
does not exist, so StringQ results in the Dynamic showing "". Thereafter StringQ evaluates to True, and Dynamic shows the text instring
.savetext
is saved across sessions by being a DynamicModule local variable.string
can be updated directly, e.g.string = "new text"
However, if more than one copy of this construction is open at the same time, one
string
variable will update the other.It would be interesting to see suggestions on how instances could be isolated, (without renaming 'string').