5
$\begingroup$
ab := {{a, 10, 100}, {b, 0, 10}}
Manipulate[a*b, Evaluate[Delete[ab, 0]], Button["SHow", Print[Evaluate[Symbol[ToString[ab[[1]][[1]]]]]]; Print[a, "---", b]]]

In the above example, I am having controls info in a variable ab. Now how to get the value of control variables without directly referring to a and b.

When I clicked the SHow button, I am getting the value if I explicitly print a & b. However I want to get these values using ab.

I tried Evaluate[Symbol[ToString[ab[1][1]]]], but it is not working.

Is there any other simple way? enter image description here

$\endgroup$
6
  • $\begingroup$ It's not clear what you want. What do you want? Where do you want to get the value of the control variables? $\endgroup$ Commented Oct 29, 2015 at 7:04
  • $\begingroup$ @PatrickStevens I want the value of "a" when "SHow" button is clicked . But without explicitly printing "a" $\endgroup$
    – Prashanth
    Commented Oct 29, 2015 at 7:06
  • $\begingroup$ Ah, I see. I think this is nearly a duplicate of mathematica.stackexchange.com/questions/10604/… where the answer is "make a, b parameters of a function instead". $\endgroup$ Commented Oct 29, 2015 at 7:14
  • $\begingroup$ @PatrickStevens thanks for the link. But there is a difference that in my case, I don't know about my control variables explicitly. I have to access it only through "ab". Correct me if I am missing anything $\endgroup$
    – Prashanth
    Commented Oct 29, 2015 at 7:47
  • $\begingroup$ The problem is that a and b are scoped and the full name of each is something like FE`a$$43. $\endgroup$
    – Kuba
    Commented Oct 29, 2015 at 8:10

2 Answers 2

3
$\begingroup$

The problem is that the global symbols hidden in the definition of ab are not localized in the Manipulate. Manipulate reassigns the a and b that appear in the code passed to Manipulate by Evaluate[Delete[ab, 0]] to localized DynamicModule symbols, which are then remapped to front-end dynamic variables whenever the Manipulate output is instantiated in the front end. (It's a complicated process, which you can read about in the four tutorials on Manipulate and dynamic functionality, as well as on this site.)

So one easy way to get the sample code to work the way that is asked is to evaluate ab before it is passed to Manipulate. The OP's ultimate goal seems unclear, not only to me but to some commenters. It is possible that in a more complicated use-case, the following might not work due to reasons specific to the use-case. However, it works on the present example.

ab := {{a, 10, 100}, {b, 0, 10}}
Manipulate[a*b, Evaluate[Delete[ab, 0]], 
 Evaluate@With[{ab = ab}, 
   Button["SHow", Print[ab[[1, 1]]]; 
    Print[a, "---", b]]
   ]
 ]

Mathematica graphics

Update notice: Originally, I had simply modify the Button to remove the Symbol thus: Print[Evaluate[ToString[ab[[1]][[1]]]]. While that was accepted, on second thoughts, I thought printing the actual value instead a string representation seemed more in line with the wording of the question.

$\endgroup$
2
  • $\begingroup$ It would be nice to have ParentDynamicModuleNumber[] function finally. $\endgroup$
    – Kuba
    Commented Oct 30, 2015 at 10:36
  • $\begingroup$ @Kuba Wouldn't it though? $\endgroup$
    – Michael E2
    Commented Oct 30, 2015 at 10:49
1
$\begingroup$

You might consider this simple form, which cuts the Gordian knot by making each control show its current value.

ctrls := {{a, 10, 100}, {b, 0, 10}};
Manipulate[a*b,
  Evaluate[Sequence @@ (Append[#, Appearance -> "Labeled"] & /@ ctrls)]]

demo

This will work no matter how many controls are specified in the list curls as long as each element of the list has the proper form.

$\endgroup$
1
  • $\begingroup$ Thanks for the answer. But my question is how to get the value after pressing the button. Basically I want to pass the controls' value (value of "a" & "b" in your example) to a function and I am not able to access its value by using controls list (which is "ctrls" in your example) $\endgroup$
    – Prashanth
    Commented Oct 30, 2015 at 0: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.