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.

In a nutshell, why does this work (change y and see how the value updates)

ClearAll[y];
Dynamic[x,
 Initialization :> (x = Symbol["y"])]

and then on a separate cell

y = 98;

but this doesn't? (assign the printed variable whatever value and see how it doesn't update)

Dynamic[x,
 Initialization :> (x = Unique[])]

You can also write Dynamic[the printed var] in a separate cell and see how it doesn't update either.

For now, I'll be using something that works and I would have never thought I would be using

Dynamic[x,
 Initialization :> (x = (Remove[#]; Symbol[#]) &@ToString@Unique[])]

My current weird conclusion: "If a variable was declared by Unique[] inside a Dynamic update, then it will never be tracked by any Dynamic "

share|improve this question
    
Does using Unique["xx"] make any difference? –  Mr.Wizard Aug 11 '12 at 14:35
    
@Mr.Wizard no it doesn't. Neither does Unique["Global`blah"] –  Rojo Aug 11 '12 at 14:38
3  
This has to do with Dynamic executing asynchronously. When given together in one cell block y=98 is already executed before the Dynamic is fully finished. –  Sjoerd C. de Vries Aug 12 '12 at 7:06
2  
I give up. Shall we call it a bug? –  Sjoerd C. de Vries Aug 12 '12 at 21:34
3  
@SjoerdC.deVries sure. I should mail Wolfram and see what comes back –  Rojo Aug 12 '12 at 23:11
show 16 more comments

1 Answer 1

I attempted this

With[{z = Unique[]}, Dynamic[w, Initialization :> (w = z)]]

and it prints \$nnn for some nnn. Then if you update the var \$nnn in other cell it gets updated. This may be a better code that the one you found.

I thought about why, but couldn't explain it. It seems Dynamic understands Symbol but doesn't understand Unique. The introduction of With seems to solve the shortcoming in Dynamic and I was speculating about a syntactic construct there but deleted my reply as it doesn't explain why Dynamic still works with Symbol.

Anyway, hope helps.

share|improve this answer
    
Thanks. This solves the updating issue, probably because the Unique isn't called during a dynamic update. However, for the same reason, it doesn't serve the same purpose as the original code. If you run this code, then Quit the kernel, and then start using Unique or perhaps even Module somewhere else, you might end up with the same variable name as in this dynamic cell... –  Rojo Jan 8 '13 at 13:45
    
I'm actually thinking of posting and accepting an answer such as "variables declared by Unique inside a Dynamic can't be tracked. I don't know if this is intended behaviour, but based on the comments it seems it isn't, and it has been reported". I was just hoping of some miracle explanation or setting –  Rojo Jan 8 '13 at 13:46
    
@rojo, I believe you should say "symbols created with Unique" instead of variables. The oddity is that symbols created with Symbol still work. So I bet Dynamic does more than a simple syntactic (but at runtime) resolution of dependency. –  caya Jan 8 '13 at 20:38
add comment

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.