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

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
POSTED BY: Szabolcs Horvát
POSTED BY: Szabolcs Horvát
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