3

I want to make export of a plot inside Manipulate, but only once. However, this code runs infinitely and does export infinitely:

Manipulate[Module[{pl},
  pl = Plot[Sin[a x + b], {x, 0, 6}];
  Export["pl.pdf", pl];
  pl]
 , {a, 1, 4}, {b, 0, 10}]

If I remove Export[] from Manipulate, problem disappears, but I need it inside Manipulate for other reason;)

Can we control number of evaluations of certain code inside Manipulate?

Murta
  • 26,275
  • 6
  • 76
  • 166
Cendo
  • 681
  • 3
  • 13
  • Can you please add version information? Which OS and which Mathematica version you are using? – Nasser Aug 14 '13 at 17:23

2 Answers2

3

It is a Dynamic problem. Export seems to be tagged dynamic somewhere. This is workaround. When I saw this, I remembered similar problem few days ago, and I used the same trick given there and it worked.

I set SynchronousUpdating -> False and ContinuousAction -> False else it will be very slow since you'll be trying to make a pdf file for each small tick change. Now a PDF file will be created when you release the mouse only from the slider.

SetDirectory[NotebookDirectory[]]; (*so we know where the PDF file is *)

Manipulate[

 Module[{x},
  pl = Plot[Sin[a x + b], {x, 0, 6}];
  myExport["myPlot.pdf", pl];
  pl
  ],

 {a, 1, 4},
 {b, 0, 10},

 SynchronousUpdating -> False,
 ContinuousAction -> False,

 Initialization :> 
  (
   myExport[type_String, p_] := myExport[type, p] = Export[type, p]
   )
 ]

reference: Manipulate keeps updating due to a function

Update

The problem as I saw it, is why Manipulate continue to update when using an Export when though Export is clearly not updating any of the control variables. On V 9.01, these are animations showing the problem. First animation is the original code posted above, and the second one, after adding SynchronousUpdating -> False.

The real question I assumed why: Why does this happen.

enter image description here

and

enter image description here

Nasser
  • 143,286
  • 11
  • 154
  • 359
3

What about this (sans Module):

Manipulate[pl = Plot[Sin[a x + b], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}, 
Button["Export", Export["pl.pdf", pl]]]

Mathematica graphics

Yves Klett
  • 15,383
  • 5
  • 57
  • 124
  • "Clean clean" ♪ ♫ +1 – Mr.Wizard Aug 14 '13 at 16:04
  • @Mr.Wizard whuzzat (non sequitur)? – Yves Klett Aug 14 '13 at 16:07
  • 1
    Well, I looked at your answer and thought "that's clean." Then I thought of this. Yes, I know I'm weird. :o) – Mr.Wizard Aug 14 '13 at 16:09
  • LOL! Now I have to get that out of my head, thank you :-) – Yves Klett Aug 14 '13 at 16:14
  • But really the question was on why using Export was causing a Continuous updating since it should not be the case. I say this is bug or bad design somewhere in export. The 2 solutions given are simply work around. The problem itself has not gone away, because again, calling export[] should not have caused this. Even when adding SynchronousUpdating -> False which means do not time out, the Continuous updating is still there. – Nasser Aug 14 '13 at 16:58
  • @Nasser Wait a minute, you mean it's exporting even when you're not moving the sliders? I do not have that problem in v7. – Mr.Wizard Aug 14 '13 at 17:01
  • I mean, if you add SynchronousUpdating -> False and do nothing, you'll see the cell bar on the right busy. even if you wait 20 minutes, it is still busy. Clearly the PDF has been exported by now. Yet it is busy. So this means it is calling itself again and again, but that should NOT be the case, as one is not touching the sliders. That is what I mean. The problem has not been solved. These are just work arounds. If a dynamic object has the setting SynchronousUpdating->False, the front end will queue up its evaluation, and allow it to be performed asynchronously – Nasser Aug 14 '13 at 17:05
  • @Nasser I am presuming the OP simply wants to export at will at some point and not automaically after each update. – Yves Klett Aug 14 '13 at 17:09
  • Well, they did put the Export inside the Manipulate expression. So this meant to me, they wanted to export a new pdf file for each change in the plot they make when moving the slider. Else why put the export inside? But this is besides the issue. The real problem remains. Manipulate should not be updating all the time as it does, in particular, when adding SynchronousUpdating->False. This is my point. Export is not updating the control variables any where. So there is no reason for Manipulate to keep calling itself. – Nasser Aug 14 '13 at 17:14
  • @Nasser Since I'm not seeing the behavior you describe this is (apparently) version specific; please tag the question accordingly. – Mr.Wizard Aug 14 '13 at 17:20
  • @Mr.Wizard I do not know what version the OP has. I'll add a comment to OP to add version information. I am on V 9.0. I'll add an animated GIF in my answer to show the problem also. – Nasser Aug 14 '13 at 17:22