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.

This question already has an answer here:

I defined a function, solve, with a free variable n, which changes while solve is running. Therefore, evaluating

Dynamic[n]

solve[];

allows me to see the changes. But when I put solve into any sort of interface element like a button, I dont see n changing. Even if I evaluate

Dynamic[n]

bn = 0;
Dynamic[If[bn === 1, {bn = 0, solve[x]}]]

bn = 1;

I only see n change when the evaluation of solve is complete. What do I need to do to be able to observe changes to n as they happen?

Edit

With this, I see n change as evaluation progresses:

n = 0; solve[] :=  Do[n += 1, {1000000}]; Dynamic[n]; solve[]

With this, I only see n change at the end when it reaches 1000000:

bn = 0; Dynamic[If[bn === 1, {solve[], bn = 0}]]; bn = 1;
share|improve this question
3  
Please post the code defining solve. Without this we don't have enough information to help you. –  m_goldberg Feb 10 '13 at 2:57
    
n = 0; solve[] := Do[n += 1, {1000000}]; Dynamic[n] solve[] <- This makes n changes along progress Dynamic[If[bn === 1, {solve[], bn = 0}]]; bn = 1; <- here i see n only change at end to 2000000 –  søren Feb 10 '13 at 9:50
    
Please do not put requested example code in a comment. Add it to your original question as an edit. This time, because you are new to Mathematica.SE, I did it for you to show how it's done. –  m_goldberg Feb 10 '13 at 15:17
    
We've had quite a few similar questions. For example, see this one –  m_goldberg Feb 10 '13 at 15:25
add comment

marked as duplicate by m_goldberg, Sjoerd C. de Vries, Ajasja, Oleksandr R., Verbeia Feb 13 '13 at 7:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

Since you did not show some code, I am here guessing what your problem can be from your mention of the use of a Button.

First, a direct example that works as is:

n = 0;
Dynamic[n]
solve[] := Module[{i},
  Do[n++; Pause[.21], {i, 10}]
  ]

Now, if you put this in a Button as is, you will only see the final result:

n = 0;
Dynamic[n]
Button["click me", Do[n++; Pause[.2], {i, 10}]]

However, to see n changes after you click the button, you need to set the Method option:

n = 0;
Dynamic[n]
Button["click me", Do[n++; Pause[.2], {i, 10}], Method -> "Queued"]

Now you will see n changes.

share|improve this answer
    
Thank you , Method -> "Queued" worked –  søren Feb 10 '13 at 10:03
add comment

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