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've made a peak-fitting GUI in which I have a "Fit" button that, when pressed, calls NMinimize[...,StepMonitor->(step++)] to minimize a sum of squared deviations. Elsewhere in the GUI I have a Dynamic[ToString[step]] so that the user can monitor the progress of the fitting. For whatever reason, the string only gets updated at the end of the NMinimize computation and not during.

Rather than post my own enormous code here, I've put together a simple example that exhibits the same behavior:

vars = {0, 0, 0, 0};
{
Button["DO STUFF", (

data = {
       {+0.18,-0.13}, {+0.84,-0.06}, {+0.05,+0.88}, {+0.24,-0.63}, {+0.67,+0.93}, 
       {+0.05,+0.88}, {+0.65,+0.92}, {+0.01,+0.99}, {+0.17,-0.04}, {+0.23,-0.55}
       };

model[{a_, k_, w_, p_}] = a Exp[-k x] Sin[w x + p];

FindFit[
       data,
       model[{a, k, w, p}],
       {a, k, w, p},
       x,
       StepMonitor :> (vars = {a, k, w, p}; Pause[0.3];)
       ]
)],
Dynamic[vars]
}

If I evaluate the above code and press the button, nothing happens for a while and then suddenly the Dynamic[vars] updates with the final value of vars.

If, on the other hand, I remove the code from the button and evaluate it on its own then the Dynamic[vars] updates as expected:

vars = {0, 0, 0, 0};

data = {
       {+0.18,-0.13}, {+0.84,-0.06}, {+0.05,+0.88}, {+0.24,-0.63}, {+0.67,+0.93}, 
       {+0.05,+0.88}, {+0.65,+0.92}, {+0.01,+0.99}, {+0.17,-0.04}, {+0.23,-0.55}
       };

model[{a_, k_, w_, p_}] = a Exp[-k x] Sin[w x + p];

FindFit[
       data,
       model[{a, k, w, p}],
       {a, k, w, p},
       x,
       StepMonitor :> (vars = {a, k, w, p}; Pause[0.3];)
       ];

Dynamic[vars]

The same thing happens if I replace the Button[] with a homemade button using EventHandler[]. Can anybody tell me why this happens and if there's a way around it? I suspect that it's a scope issue or a front end vs. kernel thing but I don't know much about Mathematica under the hood.

Thanks in advance for any help that may be offered!

P.S. I'm using MMA version 8.0 on an Intel Mac.

share|improve this question
1  
The disappearance of your opening greeting isn't a glitch... StackExchange just disagrees with the inclusion of pleasantries in questions. Anyway, welcome to the site! –  Oleksandr R. Mar 16 '13 at 2:43
add comment

marked as duplicate by m_goldberg, Mr.Wizard Mar 16 '13 at 10:24

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

up vote 2 down vote accepted

Simple solution is: you have to use Method->"Queued" as option to button

vars = {0, 0, 0, 0};
{Button["DO STUFF", (data = {
{+0.18, -0.13}, {+0.84, -0.06}, {+0.05, +0.88}, {+0.24, -0.63}, 
{+0.67, +0.93}, {+0.05, +0.88}, {+0.65, +0.92}, 
{+0.01, +0.99}, {+0.17, -0.04}, {+0.23, -0.55}};
   model[{a_, k_, w_, p_}] = a Exp[-k x] Sin[w x + p];
   FindFit[data, model[{a, k, w, p}], {a, k, w, p}, x, 
    StepMonitor :> (vars = {a, k, w, p}; Pause[0.3];)]), 
  Method -> "Queued"], Dynamic[vars]}
share|improve this answer
    
Thank you. I looked at Method->"Queued" in the documentation but I didn't think it was applicable. It certainly works for this example. I'll try it on my larger code and see if it fixes the problem. Many thanks! –  Adam W. Mar 15 '13 at 23:44
    
Works great in my larger code as well. Thanks again. –  Adam W. Mar 15 '13 at 23:55
add comment

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