Message Boards Message Boards

Update AppendTo when interacting with PopupMenu?

Posted 5 years ago

I'm trying to ListPlot a list calculated by the Button called "Plot" with PopupMenu linked to ListPlot to control the temperature units of displayed data, for example: if "DegreesCelsius" -> "C" is selected from the list then ListPlot should show plotted curve in "DegreesCelsius" and the same goes for "DegreesFahrenheit". AppendTo is used here to ListPlot new list every time the input of InputField is changed and Button is pressed by appending it to the old one.

My Problem is that the temperature units of the plotted curves don't change when selecting different values from PopupMenu. AppendTo doesn't update accordingly. I can see that popupSelection variable changes when interacting with PopupMenu but plotData variable only changes when Button is pressed, using current PopupMenu selection.

If I remove AppendTo from Button and include dataSet4:=Transpose[...] instead, as well as keeping it in Initialization everything works as I wanted. I tried to make several changes by shifting things around but nothing helped.

Please Help!

The example below demonstrates the issue.

DynamicModule[{},
 inputList = {{1, 5}, {2, 4}, {3, 8}};
 Dynamic@Column[{
    InputField[Dynamic[inputData]],
    Dynamic@Button["Plot",
      dataSet1 = Map[Norm, Differences[inputList]];
      dataSet2 = Exp[-N[Pi]*0.345*5*dataSet1/(10*2000)];
      dataSet3 = FoldList[10 + (#1 - 10)*#2 &, inputData, dataSet2];
      AppendTo[plotData, dataSet4]], Spacer[5],
    PopupMenu[Dynamic[popupSelection], {"DegreesCelsius" -> "C", "DegreesFahrenheit" -> "F"}],
    ListPlot[plotData, Joined -> True, ImageSize -> {300}, Frame -> True]}],
 Initialization :> (
   popupSelection = "DegreesCelsius";
   inputData = 0;
   plotData = {};
   dataSet3 = {0};
   dataSet4 := Transpose[{inputList[[All, 1]], QuantityMagnitude@UnitConvert[QuantityArray[dataSet3, "DegreesCelsius"], popupSelection]}];),
 SynchronousInitialization -> False]
Attachments:
4 Replies

This one updates the degrees:

DynamicModule[{inputList, inputData = 0, plotData = {}, 
  dataSet3 = {0, 0, 0}, dataSet1, dataSet2, dataSet4},
 inputList = {{1, 5}, {2, 4}, {3, 8}};
 Dynamic@Column[{InputField[Dynamic[inputData]],
    Dynamic@Button["Plot",
      dataSet1 = Map[Norm, Differences[inputList]];
      dataSet2 = Exp[-N[Pi]*0.345*5*dataSet1/(10*2000)];
      dataSet3 = Quantity[FoldList[10 + (#1 - 10)*#2 &,
         inputData, dataSet2], "DegreesCelsius"];
      dataSet4 = Transpose[{inputList[[All, 1]], dataSet3}];
      AppendTo[plotData, dataSet4]],
    Spacer[5],
    PopupMenu[
     Dynamic[popupSelection], {"DegreesCelsius" -> "C", 
      "DegreesFahrenheit" -> "F"}], 
    ListPlot[
     plotData /. dgs_Quantity :> UnitConvert[dgs, popupSelection], 
     Joined -> True, ImageSize -> {300}, Frame -> True]}],
 SynchronousInitialization -> False]
POSTED BY: Gianluca Gorni

The UnitConvert is only done at the beginning. It is not dynamic.

POSTED BY: Gianluca Gorni

I tried including expression with UnitConvert inside the Button but the output was exactly the same, AppendTo didn't update when interacting with PopupMenu

Gianluca thank you so much!!!

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