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 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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  Steve D Feb 9 '13 at 1:42
    
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. –  Kuba Feb 9 '13 at 8:40
    
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. –  Steve D Feb 12 '13 at 7:17

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.