Message Boards Message Boards

How to dynamically update the condition parameter of an If[ ]

Posted 10 years ago

Hi. I am producing a plot which data comes from a file uploaded by the user, but Mathematica tries to produce the plot from the beginning, when the data is not uploaded yet (I know I could split the procedures in different cells, but I want to keep them in a single one because, even when I know Import[ ] is not compatible with Manipulate[ ], I still want it for the web and minimize the interaction with the user).

I tried to combine and If[ ] and Dynamic[ ]. The next is a simple code that resembles what i ultimately want.

Dynamic[a, Initialization -> (a := "")]
If[Dynamic[a] =!= "", Print[Dynamic[a]], Print["Hi Five!"]]

The problem is that it never enters the "else section". It doesn't matter if I use "", or a number, or a Head or Null, it only updates for the first Print like if a were never compared inside the If.

We can see the same problema with a specific number and without the else section:

Dynamic[a, Initialization -> (a := 3)]
If[Dynamic[a] =!= 3, Print[Dynamic[a]]]

It always prints the value of "a", no matter if I set "a=3" or not.

By the way, I used to use != for "not equal", but if I use that now it doesn't do anything, just shows the If in plain text. I found on the internet that I should use =!= but I don't know why.

Thanks in advance,

Daniel

POSTED BY: Daniel Solis
4 Replies

All of If needs to be wrapped in Dynamic here:

Dynamic[
  If[a =!= "", a, "empty"]
]

Print (or other functions with side effects) isn't really suitable for use with Dynamic because the time when it gets evaluated is a bit random and depends on things like what is visible on screen.

Generally, Dynamic is used in one of two ways:

  1. Wrap a complete expression that will be displayed on screen. It will be re-evaluated and the display will be updated whenever some part of the expression changes. (In this case: when a changes.)

  2. Use Dynamic to update a variable using a control, e.g. Slide[Dynamic[x], {0,1}]. This is really a completely different thing that happens to have the same name. I think this second use is what confuses people and makes them try using Dynamic the way you did.

Other than use case 2., Dynamic is not normally used deep inside an expression. It should always wrap something that's meant for display. Also keep in mind that once inserted, Dynamic will stay in an expression. Dynamic[a] === "" will never return True. Dynamic[a] === b will only return True if the value of b is literally Dynamic[a], not when the values of a and b are identical. These uses don't trigger errors because of the flexible nature of the language, but they are incorrect nevertheless.

POSTED BY: Szabolcs Horvát

Daniel, there's nothing wrong with cross posting. Just be sure to cross-link the two posts :-)

POSTED BY: Szabolcs Horvát

If you cross post to another site, please always link the posts together! It avoids duplication of effort.

POSTED BY: Szabolcs Horvát

Sorry Szabolcs, I didn't know in what forum to post so I did in both. My mistake. I already deleted the other post and it won't happen again.

And thank you very much for the answer, I was misunderstanding the functionality of Dynamic[]. I'll make changes to my program with this new conception.

Daniel Solís

POSTED BY: Daniel Solis
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