3
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ Can you please add version information? Which OS and which Mathematica version you are using? $\endgroup$
    – Nasser
    Commented Aug 14, 2013 at 17:23

2 Answers 2

3
$\begingroup$

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

$\endgroup$
11
  • $\begingroup$ "Clean clean" ♪ ♫ +1 $\endgroup$
    – Mr.Wizard
    Commented Aug 14, 2013 at 16:04
  • $\begingroup$ @Mr.Wizard whuzzat (non sequitur)? $\endgroup$
    – Yves Klett
    Commented Aug 14, 2013 at 16:07
  • 1
    $\begingroup$ Well, I looked at your answer and thought "that's clean." Then I thought of this. Yes, I know I'm weird. :o) $\endgroup$
    – Mr.Wizard
    Commented Aug 14, 2013 at 16:09
  • $\begingroup$ LOL! Now I have to get that out of my head, thank you :-) $\endgroup$
    – Yves Klett
    Commented Aug 14, 2013 at 16:14
  • $\begingroup$ 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. $\endgroup$
    – Nasser
    Commented Aug 14, 2013 at 16:58
3
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.