8
$\begingroup$

Background: In a module containing an outer / inner manipulate I select the key of data with a dropdownlist in the outer manipulate. Data is read and displayed in the inner manipulate for edit purposes. The outer manipulate contains buttons, one of which is a save button. I want to get the edited data back at that level so that it can be saved.

Consider the following simplified example:

  Manipulate[
   Column[{j,
   Manipulate[k, {k, 1, 5, 1}],
    Button["Check", Print[k]]
   }],
  {j, 1, 10, 1}]

the question then becomes: "What is the best way to get the variable k scoped to the first / outer manipulate" ?

$\endgroup$

2 Answers 2

7
$\begingroup$

How about this:

DynamicModule[{k},
  Manipulate[
   Column[{j,
     Manipulate[k, {k, 1, 5, 1}, LocalizeVariables -> False],
     Button["Check", Print[k]]
   }],
   {j, 1, 10, 1}]
]
$\endgroup$
2
  • $\begingroup$ Just out of curiosity, is there a particular reason why you put the DynamicModule on the outside of the outer Manipulate? $\endgroup$
    – Heike
    Commented Apr 18, 2012 at 16:22
  • $\begingroup$ @Heike It is not strictly necessary, but in this way, we don't generate a new symbol/variable every time when external Manipulate recomputes its body. This is the same logic as when we use external Module to create persistent local variables, as for example I discussed here. $\endgroup$ Commented Apr 18, 2012 at 17:02
5
$\begingroup$

Using LocalizeVariables -> False as in Leonid's answer is probably the easiest, but the downside is that it makes all controls in the inner Manipulate visible to the outer Manipulate and beyond. If you have multiple controls in the inner Manipulate but you want the outer Manipulate to only see some of them to be visible to the outer Manipulate you could also do something like

Manipulate[
 DynamicModule[{k1}, 
  Column[{j, Manipulate[k1 = k, {k, 1, 5, 1}], 
    Button["Check", Print[k1]]}]], {j, 1, 10, 1}]
$\endgroup$
1
  • $\begingroup$ Heike, I have accepted Leonid's answer because what you refer to as a downside works out rather advantageous in my code. $\endgroup$ Commented Apr 18, 2012 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.