Message Boards Message Boards


[✓] Avoid issue with PopupMenu?

Posted 2 months ago
2 Replies
2 Total Likes

I have a problem with PopupMenu inside DynamicModule, its values don't update dynamically as they supposed to. The example below demonstrates the issue.

DynamicModule[{inputData = 0, dataSet1 = {0}, popupSelection, value1, value2 }, 
    Button["Plot", dataSet1 = Table[inputData + i^j, {i, 5}, {j, 2}]],
    PopupMenu[Dynamic[popupSelection], {value1 -> "v1", value2 -> "v2"}],
    ListPlot[popupSelection, Joined -> True, ImageSize -> {300}, 
     Frame -> True]}],
 Initialization :> (
   value1 = dataSet1;
   value2 = dataSet1 + 1;
 SynchronousInitialization -> False]

The PopupMenu is linked to ListPlot, for example: if value1 with label "v1" is selected from the list then ListPlot should print the evaluated value of value1 and the same goes for value2.

The problem I have is that value1 and value2 which correspond to popupSelection don't update dynamically when changing the input of InputField and pressing button "Plot". These values evaluate only once (upon DynamicModule evaluation) using initial values and they do not re-evaluate when required. I tried to wrap expressions of value1 and value2 with Dynamic but it conflicts with ListPlot giving me bunch of error messages.

I know i'm missing something here, I just can't figure out what it is. Please help!

2 Replies

I would do it this way:

DynamicModule[{inputData = 0, dataSet1 = {0, 1}, popupSelection}, 
    Button["Plot", dataSet1 = Table[inputData + i^j, {i, 5}, {j, 2}]],
     Dynamic[popupSelection], {"value1" -> "v1", "value2" -> "v2"}], 
    ListPlot[If[popupSelection === "value1", dataSet1, dataSet1 + 1], 
     Joined -> True, ImageSize -> {300}, Frame -> True, 
     Axes -> False]}],
 SynchronousInitialization -> False]

It resolves my problem, Thanks.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract