Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Output from a Dynamic Module

122 views
Skip to first unread message

Hugh Goyder

unread,
Mar 20, 2009, 3:41:07 AM3/20/09
to
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"]

Sjoerd C. de Vries

unread,
Mar 21, 2009, 6:18:17 AM3/21/09
to
Hugh,

To start with your last question, the solution would be to remove the
Dynamic from within the Export function.

As to your main question: I guess you don't want to use a global
variable? Indeed, that woud defeat the purpose of the module, i.e. to
hide the variable pts from the global scope. I don't have an elegant
solution. Perhaps copying pts to a global variable if it is your
intention to hide pts?

Cheers -- Sjoerd

> Axes -> False, PlotRange -> {All, {-1, 1}}, ImageSize -> 7 72=
]]
> ]
> }]
> ]
>
> pp = Import["temp"]


John Fultz

unread,
Mar 21, 2009, 6:18:39 AM3/21/09
to
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
jfu...@wolfram.com
User Interface Group
Wolfram Research, Inc.

Yves Klett

unread,
Mar 21, 2009, 6:15:14 AM3/21/09
to
Hugh,

perhaps something like:

DynamicModule[{pts}, pts = {{0, 0}, {1, 0}, {2, 0}, {3, 0}};
Column[{Button["Assign points",

CellPrint[ExpressionCell[pts, "Input"]]],


LocatorPane[Dynamic[pts],
Dynamic[Plot[Interpolation[pts][x], {x, 0, 3}, Frame -> True,
Axes -> False, PlotRange -> {All, {-1, 1}},
ImageSize -> 7 72]]]}]]

could help?

Regards,
Yves


Hugh Goyder schrieb:

Jens-Peer Kuska

unread,
Mar 21, 2009, 6:14:27 AM3/21/09
to
Hi,

SetAttributes[SaveThePoints, HoldAll]
SaveThePoints[pnts_] :=


DynamicModule[{pts}, pts = {{0, 0}, {1, 0}, {2, 0}, {3, 0}};

Column[{Button["Assign points", pnts = pts],


LocatorPane[Dynamic[pts],
Dynamic[Plot[Interpolation[pts][x], {x, 0, 3}, Frame -> True,
Axes -> False, PlotRange -> {All, {-1, 1}},
ImageSize -> 7 72]]]}]]


and

SaveThePoints[p]

will save it to th variable p.

Regards
Jens

0 new messages