Message Boards Message Boards

Button not working with Plot?

Posted 2 years ago

Hi everyone, I am totally new to Mathematica, so please forgive me my probably stupid question.

I am trying to do some Plots of functions with RandomReals in them. So every run it will plot a bit different. I want to run the script from a button, but it does not work and in the docs and examples is nothing that has illuminated me. Also on youTube I find tons of movies, but not about how to implement buttons.

This works (from the examples) :

Button["Click Here", Print[10!]]

A simple

Plot[Sin[x], {x, 0, 6 Pi}]

also works,

But why does this NOT work :

Button["Click Here", Plot[Sin[x], {x, 0, 6 Pi}]]

It creates the button, which does not do anything when clicked. I do not get an error, so I have no clue.

It would be great to also get some pointers as to where I can find more info about how to pack more elaborate functions into a button. Also how to save to disc , by Button script, an Image / Graphics that has been created with Mathematica.

Thanks a lot,

POSTED BY: B. Cornas
4 Replies
Posted 2 years ago

The reason is that Button effectively returns nothing. Here is one way to do it.

plot =.;
Button["Click Here", plot = Plot[Sin[x], {x, 0, 6 Pi*RandomReal[{1, 10}]}]]
Dynamic@plot

Each time the button is clicked the plot is different because of the RandomReal.

You can use Export to save an Image or Graphic. How that is linked to the Button depends on how the Image or Graphic is created.

POSTED BY: Rohit Namjoshi

This also works:

Button["Click Here", 
 Print[ Plot[Sin[x], {x, 0, 6 Pi*RandomReal[{1, 10}]}] ]
]
POSTED BY: Gustavo Delfino
Posted 2 years ago

A free on-line Wolfram training course is starting next week covering that subject :

Training course

POSTED BY: Yvan Abbe
Posted 2 years ago

Thanks a lot, I am getting it :-)

POSTED BY: B. Cornas
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