Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to use Button as a controller in Manipulate.

I have done some thing like this:

Manipulate[
  p = Column[{Plot[Sin[x - n], {x, 0, 12.4}, ImageSize -> 500]}], 
  {n, 0, 1}, 
  Button["Export", Export["test.jpg", p]]]

Is there any better way to do this?

I am thinking of making something like this:

Manipulate[
  Column[{p = Plot[Sin[x - n], {x, 0, 12.4}, ImageSize -> 500], 
  If[x, Export["test.jpg", p]]}], 
  {n, 0, 1}, 
  {x, {False, True}}]

in which the controller x is represented by Button instead of a Checkbox.

How can I do that?

share|improve this question
    
there is a button example in the docs ref/Manipulate#163569372 –  Mike Honeychurch Jun 12 at 22:19

1 Answer 1

Personally, I would stay with your first example, but if you insist on a more loose coupling between the button and the Export expression, maybe something like the following:

filePath = FileNameJoin[{HomeDirectory[], "DeskTop", "test.jpg"}];
Manipulate[
  p = Plot[Sin[x - n], {x, 0, 4 Pi}, ImageSize -> 300];
  If[export, Export[filePath, p]; export = False];
  p,
  {n, 0, 2 Pi, Appearance -> "Labeled"},
  {{export, False}, None},
  Button["Export", export = True]]

manip

share|improve this answer
    
There is x as Boolean, and also x in the plot range value (argument to Plot)? –  Nasser Jun 13 at 0:53
    
@Nasser. They are not the same; they are in different scopes. Maybe bad naming, but I used the OP names. –  m_goldberg Jun 13 at 0:59
    
Yes, I know, Plot localized the x, else it wont work, but I meant a different name would be better for readability and less confusing. that is all. –  Nasser Jun 13 at 1:06
    
@Nasser. I agree. I'll do an edit. –  m_goldberg Jun 13 at 1:15
1  
@Algohi You can put the calculations in the button action. You might have to add Method -> "Queued" in case it does not time-out if it takes too long. "Queued" method does not time out. But you can try and see which works best, the default (preemptive) or the Queued for your application. –  Nasser Jun 13 at 2:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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