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 have the following code:

a = Dynamic[Floor[Clock[2]]] (*So this basically alternates between 0 and 1*)
If[a==1, Print["true"]]

But this doesn't work, it instead prints the following If[a==0, Print["true"]] and then changes to If[a==1, Print["true"]] and then back.

How can I get the value from the clock?

share|improve this question

marked as duplicate by Mr.Wizard Apr 8 '13 at 10:22

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.

    
Related: mathematica.stackexchange.com/q/2972/121 –  Mr.Wizard Apr 8 '13 at 10:25
add comment

2 Answers 2

up vote 3 down vote accepted

Well, looking at the other answers, I am not sure if I correctly understand what you want. But I'll give it a go:

a = Dynamic[Floor[Clock[2]]](*same thing*)
Dynamic[If[Setting@a == 1, "true", "false"]]

Does this yield what you wanted? The problem in your case is, that a is no "plain" integer, see FullForm[a]. Thus, the test a==1 is not executing. Using Setting lets you retrieve the value of a Dynamic var, see e.g. here: Dynamic Values

share|improve this answer
    
Thank you very much, this works! –  user1626227 Apr 8 '13 at 8:11
add comment

Well, the problem locates in the difference between Equal and SameQ. When Equal can't determin whether the args are equal or not, it returns the expression back with no changes. What you really need is SameQ here, and the code looks like this:

a = Dynamic[Floor[Clock[2]]] (*So this basically alternates between 0 and 1*)If[a === 1, Print["true"]]
share|improve this answer
add comment

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