Message Boards Message Boards

0
|
3804 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How can we use While inside Manipulate?

Posted 9 years ago

Below is code which is supposed to create a window with a graphic, and then refresh is every 3 seconds until the Control "run" is unchecked. It creates the window and does the continuous refresh, but unchecking "run" has no effect. Can any of you please point out what I'm doing wrong?

Thanks

run = True;

window = CreateDocument[Dynamic[graphic], WindowSize -> {600, 450}, 
   TextAlignment -> Center, ShowCellBracket -> False, 
   WindowFrame -> "Frameless", WindowFrameElements -> {}];

Manipulate[
 While[run,
  points = Table[RandomReal[{-1, 1}, 2], 10];
  graphic = 
   ListPlot[points, PlotRange -> {{-1, 1}, {-1, 1}}, PlotLabel -> run];
  Pause[3];
  ];
 NotebookClose[window];
 Abort[],
 {run, {True, False}}
 ]

run

NotebookClose[window]
POSTED BY: David Keith
3 Replies

I think the problem you're having is that Pause is a blocking operation, which results in it messing up the Manipulate. We can replace it and the While with a Dynamic with UpdateInterval and an If.

Manipulate[
 Dynamic[If[run, points = Table[RandomReal[{-1, 1}, 2], 10];
    graphic = 
     ListPlot[points, PlotRange -> {{-1, 1}, {-1, 1}}, 
      PlotLabel -> run];, NotebookClose[window];
    Abort[];];, UpdateInterval -> 3], {{run, True}, {True, False}}]

window = CreateDocument[Dynamic[graphic], WindowSize -> {600, 450}, 
   TextAlignment -> Center, ShowCellBracket -> False, 
   WindowFrame -> "Frameless", WindowFrameElements -> {}];

run

I switched the Manipulate and the window definition because I thought it makes more sense this way, but you can move them back if you want.

POSTED BY: Jesse Friedman
Posted 9 years ago

Thank you, Jesse! Works great, and I learned something new!

POSTED BY: David Keith

No problem!

POSTED BY: Jesse Friedman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract