Re: Output from a Dynamic Module
- To: mathgroup at smc.vnet.net
- Subject: [mg97779] Re: [mg97748] Output from a Dynamic Module
- From: John Fultz <jfultz at wolfram.com>
- Date: Sat, 21 Mar 2009 05:18:55 -0500 (EST)
- Reply-to: jfultz at wolfram.com
On Fri, 20 Mar 2009 02:41:18 -0500 (EST), Hugh Goyder wrote:
> In the toy example below I have a dynamic module where I adjust
> Locator points to form a curve. When I have got a nice set of points
> I would like to output them so that they can be used in other
> calculations. I have provided a button to do this but can't see the
> best way forward. Normally the output from a module is the last line
> but here this is not possible. I give a bad method by using a
> temporary file but what other methods are there? (And why does the
> temporary file I import get up-dated even when I don't press the
> button?)
> Thanks
> Hugh Goyder
>
> DynamicModule[{pts},
> pts = {{0, 0}, {1, 0}, {2, 0}, {3, 0}};
> Column[{
> Button["Assign points", Export["temp", Dynamic[pts], "List"]],
> LocatorPane[Dynamic[pts],
> Dynamic[
> Plot[Interpolation[pts][x], {x, 0, 3}, Frame -> True,
> Axes -> False, PlotRange -> {All, {-1, 1}}, ImageSize -> 7 72]]
> ]
> }]
> ]
>
> pp = Import["temp"]
First, you shouldn't be calling Export[] on Dynamic[pts]. You should be calling
it on pts. What you're exporting isn't the actual list of points, but a
Mathematica expression contained in a Dynamic. Try doing FilePrint["temp"] to
see what I'm talking about.
I recommend reading a post of mine from last month on Dynamic which should help
you to understand why that happened and why using Dynamic there was predictably
a bad idea...
http://forums.wolfram.com/mathgroup/archive/2009/Feb/msg00424.html
Of course, as you suspected, exporting to a temporary file is certainly not the
most convenient way. There's no reason that the Button[] can't interact with
variables outside of the Dynamic interface. You could have simply done this...
DynamicModule[{pts}, pts = {{0, 0}, {1, 0}, {2, 0}, {3, 0}};
Column[{Button["Assign points", thePoints = pts],
LocatorPane[Dynamic[pts],
Dynamic[Plot[Interpolation[pts][x], {x, 0, 3}, Frame -> True,
Axes -> False, PlotRange -> {All, {-1, 1}},
ImageSize -> 7 72]]]}]]
and then you can see the output by simply evaluating 'thePoints'.
Sincerely,
John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.