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.
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.
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:
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.)
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.
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.