Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Dynamic Interactivity > Dynamic Interactivity Language >

DynamicModule

Updated In 7 Graphic
DynamicModule[{x, y, ...}, expr]
represents an object which maintains the same local instance of the symbols x, y, ... in the course of all evaluations of Dynamic objects in expr. Symbols specified in a DynamicModule will by default have their values maintained even across Mathematica sessions.
DynamicModule[{x=x0, y=y0, ...}, expr]
specifies initial values for x, y, ....
  • DynamicModule first gives unique names to local variables in expr, just like Module, then evaluates the resulting expression, and then returns a version of this wrapped in DynamicModule.
  • In a notebook, this returned version will often be displayed in an output cell. An example is the output from Manipulate.
  • If interactive changing or editing of Dynamic objects within the displayed form of a DynamicModule results in changes in any values for local variables, then the DynamicModule object is modified to reflect these.
  • Values of local variables in a DynamicModule are by default automatically saved when a notebook containing the DynamicModule is saved, so that these values in effect persist across sessions of Mathematica.
  • If you make a copy of a piece of a notebook that contains a DynamicModule object, the local variables in the copy will be independent of the local variables in the original, though they will start with the same values.
  • The following options can be given:
DeinitializationNonean expression to evaluate when the DynamicModule can no longer be displayed
DynamicModuleValuesAutomaticdynamically updated data on variable values
InitializationNonean expression to evaluate when the DynamicModule is first displayed
UnsavedVariables{}variables whose values should not be saved
  • When DynamicModule is first evaluated, initial assignments for local variables are made first, and then any setting for the Initialization option is evaluated.
  • When a DynamicModule object is displayed for the first time in a particular notebook, saved values for local variables are restored, and then any setting for the Initialization option is evaluated.
  • All values of local variables are saved, except for those included in the UnsavedVariables list. Ordinary values of symbols are saved in the first argument of the DynamicModule; other values are saved in the setting for the DynamicModuleValues option.
  • DynamicModule constructs can be nested in any way, with inner variables being renamed if necessary.
  • DynamicModule is a scoping construct that implements lexical scoping.
Create a Slider with a dynamically updating localized variable:
Copy and paste the output above to get a standalone object:
Create a Slider with a dynamically updating localized variable:
In[1]:=
Click for copyable input
Out[1]=
Copy and paste the output above to get a standalone object:
Out[2]=
Out[3]=
Use DynamicModule to localize variables and prevent conflicts with their global counterparts:
Local variables are unique to the generated output:
Assign initial values to local variables, just like Module:
Define a function as a local variable:
Use DynamicModule to save the displayed state across different Mathematica sessions:
Module only gives unique states in the current Mathematica session:
Use Initialization to evaluate expressions before displaying the contents:
Initialization is evaluated after the local variable assignments:
Specify expressions to be evaluated when output is no longer displayed:
Function definitions of local symbols are automatically inserted into DynamicModuleValues:
By default, external definitions are lost between kernel sessions:
Use Initialization to save evaluations necessary for the content:
Local variables can also be initialized:
By default, the initializations are evaluated before the output is displayed (synchronously):
Force the output to display while the initializations are evaluated asynchronously:
Specify symbols whose state should not be saved:
Evaluate the following and move the two sliders, then press Close:
Evaluate the following and note that the second slider does not remember its previous state:
Construct a dynamic calculating interface:
Interactive curve fit plot:
An angular slider with range (-infty,infty):
Connect the slider to a dynamic variable:
Demonstrate a sinusoidal relationship:
Use PolarPlot to demonstrate a spiral curve:
DynamicModule and Module are fundamentally different although they seem similar:
In this case both sliders will move, and will not work without evaluation when reopened:
Manipulate relies on DynamicModule to create the output:
Independent state sliders:
Click in the framed area to see bouncing balls:
Use dynamic objects as input; click in the different frames to create additional balls:
New in 6 | Last modified in 7
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team