tags:

views:

44

answers:

2

Is it possible to use Mathematica's manipulate to change variables that have already been declared?

Example:

changeme = 8;
p = SomeSortOfPlot[changeme];
manipulate[Show[p],{changeme,1,10}]

The basic idea is that I want to make a plot with a certain changable value but declare it outside of manipulate.

Any ideas?

+2  A: 

One option is to use Dynamic[] and LocalizeVariables -> False.

Example:

changeme = 8;
p[x_] := Plot[Sin[t], {t, 1, x}];

{
 Manipulate[p[changeme], {changeme, 2, 9}, LocalizeVariables -> False], 
 Dynamic[changeme]   (* This line is NOT needed, inserted just to see the value *)
}

Evaluating "changeme" after the Manipulate action will retain the last Manipulate value.

HTH!

belisarius
You can make the controller show the value of the variable: e.g. Manipulate[p[changeme], {{changeme, 8, Dynamic[changeme]}, 2, 9}, LocalizeVariables -> False]
Simon
@Simon Yes, Thanks. I opted to show the value outside the controller because it seemed aligned with the OP request.
belisarius
@belisarius - fair enough!
Simon
+1  A: 

If you want anything reasonably complicated or flexible, it is best to use Dynamic and DynamicModule instead of Manipulate. The only exception is if you're writing a demonstration.

For example - a very basic way of doing what you want is (in fact you don't even need the Row and Slider if you want to just change changeme by hand.)

changeme=8;
p[x_]:=Plot[Sin[t],{t,1,x}];
Row[{"x \[Element] (1, ",Dynamic[changeme],")  ",Slider[Dynamic[changeme],{2,9}]}]
Dynamic[p[changeme]]
Simon
@Simon I often put the slider at the start of the Row, because when the number of digits of the Dynamic var changes, produces a ugly visual effect.
belisarius