Message Boards Message Boards

0
|
3465 Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:
GROUPS:

Infinite loop even with TrackedSymbols

Posted 10 years ago

I don't get why this two-liner gets stuck in infinity loop with TrackedSymbols option?

InputField[Dynamic[mats], String]
Dynamic[If[mats == "1", 
  CreateWindow[
   DialogNotebook[{TextCell["Not a valid matrix."], 
     DefaultButton[DialogReturn[]]}]]], TrackedSymbols :> {mats}]

And one more thing:

InputField[Dynamic[mats], String]
Dynamic[If[mats == "1", "word"], TrackedSymbols :> {mats}]

How can I supress the output Null so that if the condition is not satisfied no output is displayed? Thanks.

POSTED BY: Al Guy
6 Replies
Posted 10 years ago

Equal is not really suited to compare Strings. Use SameQ ===.

I don't know how to explain your problem with formal wording but here is how it should be done:

InputField[Dynamic[mats], String]
Dynamic[If[mats === "1",
                 CreateWindow[  DialogNotebook[{TextCell["Not a valid matrix."],   DefaultButton[DialogReturn[]]}]]; 1,
                 2], 
 TrackedSymbols :> {mats}]
POSTED BY: Kuba Podkalicki
Posted 10 years ago

I attached the file. How can I make it so that it either prints "word" or prints nothing at all, not even space, suppress any output.

Attachments:
POSTED BY: Al Guy
Posted 10 years ago

Thanks. How about the Null part of my question? Any way to suppress it?

POSTED BY: Al Guy

How about the Null part of my question? Any way to suppress it?

For the second part, I do not see a Null printed, but the actual If statement itself

enter image description here

The reason is because that is how If works when it has one branch and it is not true. For example

enter image description here

To workaround this, use the third branch of If which tells it what to do if it can't decide between True and False

enter image description here

So for your first example, to eliminate the Null you can type in a value for mats other than "1", is to give it a value to use for the second branch. Also, it is best to initialize mats to "1" for first time, so the code becomes

mats = "1";
InputField[Dynamic[mats], String]
Dynamic[If[mats == "1",
  CreateWindow[DialogNotebook[{TextCell["Not a valid matrix."], DefaultButton[DialogReturn[]]}]]
  , "waiting..."], TrackedSymbols :> {mats}]

You do not need a value for the third branch (the undecided one) since now mats is initialized to some specific value.

POSTED BY: Nasser M. Abbasi

Because mats still has the value "1". It will get unstuck when you type a new value other than "1" in the InputField and hit enter. Now the loop will stop and the button will go away when you click OK on it.

POSTED BY: Nasser M. Abbasi
Posted 10 years ago

Not really. Dialogs are not well explained in context of Dynamic or different links. For example, this one is not reevaluated:

x = 1;     
Dynamic[ If[x == 1, Print[DateList[]]; 1, 2], TrackedSymbols :> {x}]

I don't know why it behave differently.

POSTED BY: Kuba Podkalicki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract