2
$\begingroup$

I have the following code, which accepts a function via an InputField, and then gradually draws that function in a Plot using Animate:

Panel[DynamicModule[{f = "x^2-1", dummy = False}, 
Column[{Row[{InputField[Dynamic[f], String, 
   ContinuousAction -> True], 
  Dynamic[Button["Plot!", dummy = Not[dummy]]]}], 
Dynamic[Animate[
  Plot[If[x < Time, ToExpression[f], Undefined], {x, -5, 5}, 
   PlotRange -> {{-5, 5}, {-5, 5}}, 
   PlotStyle -> {Thick, Blue}], {Time, -5, 5}, 
  LabelStyle -> White, AnimationRunning -> True]]}]]]

The problem is the animation is running constantly; and setting AnimationRepetitions->1 means that even after updating the function, the plot will stop redrawing. What I'd like to do is have it so that the Plot region is only updated when the Plot! button is clicked. When it is clicked, the plot is drawn gradually, as above, but only once. To do this, I would need to be able to reset the animation from the Plot! button itself, and that doesn't seem possible. I thought Clock[] might offer an elegant solution, but I think it is also impossible to reset those.

So, is there a way to do what I describe? Namely, have a graphics region that is only updated when a certain button is clicked, and when it updates an animation runs exactly once?

$\endgroup$
1
  • $\begingroup$ Related: (17571) (not a solution) $\endgroup$
    – Mr.Wizard
    Jan 15, 2015 at 0:06

1 Answer 1

2
$\begingroup$

I had a similar problem. Try this to control the time:

test = True;

DynamicModule[{u = 0},
 Dynamic[ProgressIndicator[#, ImageSize -> {480, 15}] &@
  Dynamic@
   If[test,
     Refresh[If[u == 1, test = False;, u += 1/100], 
  TrackedSymbols :> {}, UpdateInterval -> .1], Catch@Throw[u]]
] ]

"test" is the variable responsibile for stopping and realasing time. This is not very sophisticated but it works. I'd love to see more simple way.

Other way is to use RunScheduledTask[n++,{1,Infinity}] and in the moment You want, RemoveScheduledTask@ScheduledTasks[], but I had problems with creating funtion with Module for this.

$\endgroup$
3
  • $\begingroup$ Sorry for being dense, but can you explain how this solves my problem? I don't see how I could use a button to reset that progress bar. $\endgroup$
    – Steve D
    Feb 9, 2013 at 1:42
  • $\begingroup$ You can put that Button[] in this DynamicModule so it will be able to set u=0 and test="False". I hope it is helpful, but ask if You have any doubts. $\endgroup$
    – Kuba
    Feb 9, 2013 at 8:40
  • $\begingroup$ Hi Kuba, thanks so much for your answer. I eventually got it working, and it does what I need. I also wish there was a simpler way; there must be a way to do this using Clock[], but for now, this is definitely great. $\endgroup$
    – Steve D
    Feb 12, 2013 at 7:17

Your Answer

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

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