# Dynamic Syntax - Evaluate AppendTo as the second argument

I am attempting to Append a new value into my list paramMonitor whenever the value for B changes. I had the code working perfectly, but my computer crashed without saving the document. I am now having a lot of trouble (dynamically) appending these values to my list.

My attempts:

• This updates B but does nothing to append the value {B, e, some number} to paramMonitor

Dynamic[B,
AppendTo[paramMonitor,
{B,e,Abs[model[B, e][#] & /@ test[[All, 1]] - test[[All, 2]]] // Total}]


and

• Removing the first argument 'B', this code fails to compile and simply aborts upon execution:

Dynamic[AppendTo[paramMonitor,
{B,e, Abs[model[B, e][#] & /@ test[[All, 1]] - etst[[All, 2]]] // Total}]


I believe this is a quick fix (since I had it working earlier), but I cannot seem to find the issue.

-
Before I will update the answer, please tell me how B can be changed? I mean, it is tied to some kind of controller or it is just a global variable that can be change anywhere? –  Kuba Apr 24 at 11:15
Sorry about the late response. B is changed through NonLinearModelFit (NLMF). NLMF is finding the best fit values for B and e. I have NLMF's EvaluationMonitor printing "B and e values" to the screen. So, at every new evaluation step, Dynamic[B] will update. Thanks for all the help!! –  ABBOUDR Apr 29 at 18:32
So my answer does not fit your needs quite well, right? –  Kuba Apr 29 at 18:36
Unfortunately not. Since NonlinearModel takes a very long time to evaluate, I wanted a way to monitor its progress in Real-Time. My other option is to add some commands within EvaluationMonitor, like this –  ABBOUDR May 1 at 0:51
add comment

## 1 Answer

The most important thing, and the thing that can be easily missed is that the second argument of Dynamic must be a function or a list of functions:

list = {};
Slider[Dynamic[b, (b = #; list = Join[list, {b}]) &], {1, 10, 1}]
Dynamic@list


-
Another thing that can be easily missed is that the second argument of Dynamic only seems to work reliably when the Dynamic expression is evaluated as an argument of a control or other GUI element (like the slider you used in your example). I have found that the front-end does seem to like a naked Dynamic with a 2nd argument when it encounters one at top-level such as in the example the OP gave. –  m_goldberg Apr 24 at 8:01
@m_goldberg good point, I have not though he tried to do that this way. It seems I have to write a little bit more. :) –  Kuba Apr 24 at 9:15
@m_goldberg: you say "only seems to work reliably", but does it work at all and is it supposed to? I would have said it most probably isn't even meant to work, but honestly don't know what actually is the "documented" behavior that I could expect. I also can't see that a naked Dynamic with a 2nd argument does anything relevant concerning its 2nd argument, other than quietly ignoring it. Am I missing something here? –  Albert Retey Apr 24 at 14:09
@AlbertRetey. I used tentative language because I don't know for sure. From my reading of the docs and my own experiments, I think it likely that the 2nd argument of Dynamic is not intended to be used at top-level the way the OP of this question did. My experience with naked Dynamic is exactly as you say. BTW, there is a typo in my comment. I meant to say: "does not seem to like a naked Dynamic" –  m_goldberg Apr 24 at 14:44
@AlbertRetey I agree with you, it's meant to cooperate with controllers: "during interactive changing or editing of val." I think that m_goldberg pointed that OP tried to use it in "naked" Dynamic. I'm waiting for confirmation from OP. :) –  Kuba Apr 24 at 14:44
show 2 more comments