1
$\begingroup$

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;
$\endgroup$
4
  • 3
    $\begingroup$ Please post the code defining solve. Without this we don't have enough information to help you. $\endgroup$
    – m_goldberg
    Commented Feb 10, 2013 at 2:57
  • $\begingroup$ 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 $\endgroup$
    – søren
    Commented Feb 10, 2013 at 9:50
  • $\begingroup$ 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. $\endgroup$
    – m_goldberg
    Commented Feb 10, 2013 at 15:17
  • $\begingroup$ We've had quite a few similar questions. For example, see this one $\endgroup$
    – m_goldberg
    Commented Feb 10, 2013 at 15:25

1 Answer 1

4
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thank you , Method -> "Queued" worked $\endgroup$
    – søren
    Commented Feb 10, 2013 at 10:03

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