1
$\begingroup$

I have read the Wolfram documentation on Dynamic and understood enough to suspect that what I am trying to do may not be possible, so I am hoping I am wrong.

Here is the base example that works very nicely:

DynamicModule[
 {n1 = 10},
 Panel[
  Column[{
    InputField[Dynamic[n1]],
    Dynamic[
     Plot[Sin[Pi*n1*x], {x, 1, 3},
        GridLines -> Automatic,
        ImageSize -> 300]
     ]
    }]
  ]
 ]

It creates an input dialog textbox that I can edit. When I enter a new number (3 in this case) and press return the frequency of the Sin wave changes as we would expect:

Sin.jpg

On the other hand, this code does not work, or works only partially:

Clear[list];
list = {"red", "yellow", "blue"};
DynamicModule[
 {n1 = 10},
 Column[{
   InputField[Dynamic[colour], String],
   If[list[[1]] == colour,
    n1 = 1,
    0],
   Dynamic[
    Plot[Sin[Pi*n1*x], {x, 1, 3},
        GridLines -> Automatic,
        ImageSize -> 300]
    ]
   }]
 ]

In my real code I actually have a Do loop around the If statement, but since the same problem and behaviour is exhibited by this simpler example I kept things simple. The idea is that I would like to be able to enter a string, do a search on that string by cycling through an array of names, pick the index that correponds to a match, and then use that index in the plot.

This code, instead, does not respond when I enter "red" (without the quotes), as it should. However, if I execute the cell again then it remembers the new value for colour and updates the frequency. In other words, it's not dynamic. I tried different things but nothing worked. Making colour one of the module variables, as my instinct would prompt me to do, loses even the updating when re-executing the cell since now colour is not even a global variable anymore and does not remember the last value I entered. What am I doing wrong?

Thanks

Edit:

Based on @anderstood's reply I was able to go further and encountered a interesting puzzle. The new code now is

Clear[list];
list = {"red", "yellow", "blue"};
DynamicModule[
 {n1 = 10, colour = "red"},
 Panel[
  Column[
   {
    InputField[Dynamic[colour], String],
    Dynamic[
     Do[
      If[list[[i]] == colour,
       Print["i = ", i];
       n1 = i],
      {i, 1, 3}
      ]
     ],
    Dynamic[
     Plot[Sin[Pi*n1*x], {x, 1, 3},
        GridLines -> Automatic,
        ImageSize -> 300]
     ]
    }
   ]
  ]
 ]

Note that I am not using n1 = 0 in the If statement because now I am using the Do loop. Interestingly, using a Break once the desired colour has been found does not work because Break does not seem to be able to get out of the Dynamic environment (my guess). The corresponding output shows an annoying 'Null' as the second entry in the column:

Blue.jpg

Here I found some inspiration in Answer 7 to this question How to avoid returning a Null if there is no "else" condition in an If contruct, provided by @F'x. Using

DeleteCases[{hello, apple, Null, 34}, Null]

works and returns

{hello, apple, 34}.

However, inserting DeleteCases around the curly brackets inside Column[] in the code above does NOT get rid of the Null. Interestingly, I do not think this Null is coming from the If statement (as is the case in the other question), but I think it is coming from Dynamic. In any case, DeleteCases has no effect. I can't imagine why, other than Dynamic is somehow interfering. Note that I tried putting DeleteCases and more Dynamics in different places in this code but nothing worked.

If it's possible to get rid of this Null in the output panel it would be nice. Any suggestions much appreciated.

$\endgroup$
  • 1
    $\begingroup$ You should add new questions as new questions, not by editing. However, here is the answer to your edit: to get rid of Null in If, you can use If[...,...,Unevaluated[Sequence[]]], see mathematica.stackexchange.com/questions/3700/…. $\endgroup$ – anderstood Nov 15 '16 at 16:37
  • $\begingroup$ Thanks anderstood, the follow-up question seemed sufficiently close to the first one to warrant an edit. Anyway, I had seen Unevaluated[Sequence[]] in that post, and tried it. But it did not work. I just tried it now again and it does not work for me. This is why I think the Null is not coming from the If statement. I think it must come from the Dynamic, somehow. $\endgroup$ – pdini Nov 15 '16 at 17:06
  • $\begingroup$ Indeed after a closer look I don't think it comes from If. You can try changing Do to Table to better understand what is happening (but it does not solve the issue). However this is really a completely new question. $\endgroup$ – anderstood Nov 15 '16 at 17:59
  • $\begingroup$ OK, fair enough. I don't mind starting a new question, I just didn't want to clutter the site with something that seems relatively small... Anyway, I will try a few more things with Table, thank you. $\endgroup$ – pdini Nov 15 '16 at 18:29
  • $\begingroup$ The idea behind asking a new question is that even if it seems related for you, a user could face exactly the same problem with Null and will not find it here because the original question was something else. So do your investigations, search this site for your issue, and if you couldn't solve it feel free to post a new question! You won't clutter as long as you post a question which is understandable, as simple as possible to show your problem, and search for a solution before of course. $\endgroup$ – anderstood Nov 15 '16 at 18:35
3
$\begingroup$

The following seems to work:

Clear[list];
list = {"red", "yellow", "blue"};
DynamicModule[{n1 = 10}, 
 Column[{InputField[Dynamic[colour], String], 
   Dynamic[If[list[[1]] == colour, n1 = 1, n1 = 0]], 
   Dynamic@Plot[Sin[Pi*n1*x], {x, 1, 3}, GridLines -> Automatic, 
     ImageSize -> 300]}]]

Note the n1 = 0 in the If loop, so that n1 does not keep its value of 1 if entry is "blue" or "yellow". Also, the If loop is in a new Dynamic[] since it has to be dynamically updated.

Mathematica graphics

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks @anderstood, your suggestion works for me too. I am editing my original question to add a follow-up that builds on your answer. $\endgroup$ – pdini Nov 15 '16 at 11:02
0
$\begingroup$

This is a bit confusing, as there are two threads now with similar content here and in the other thread. Based on my answer, the following code would work for your example:

Clear[list];
    list = {"red", "yellow", "blue"};
    DynamicModule[
     {n1 = 10, colour = "blue"},
     Panel[Grid[{
      {InputField[Dynamic[colour], String],
       Style[Dynamic[Do[If[list[[i]] == colour, n1 = i], {i, 1, 3}]], Transparent]},
       {Dynamic[Plot[Sin[Pi*n1*x], {x, 1, 3}, GridLines -> Automatic, ImageSize -> 300]]}
      }]]
    ]
$\endgroup$
  • $\begingroup$ Indeed, thanks Stitch, as shown above I wasn't sure whether or not to create a new question but then anderstood convinced me that it was OK. I accepted your answer in the other thread. $\endgroup$ – pdini Nov 15 '16 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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