0
$\begingroup$

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]

Cross-posted on Wolfram Community

$\endgroup$
  • $\begingroup$ The variable popupSelection only influences what dataSet4 evaluates to when the button is clicked and dataSet4 in turn is only appended to plotData. How is popupSelection then supposed to influence all the values that were already stored in plotData?. You'd need to recalculate all elements in plotData for this to work. Is it not easier to just keep the data as quantities and only convert them right before plotting? $\endgroup$ – Sjoerd Smit Mar 15 at 17:00
  • $\begingroup$ I'm new to Mathematica and I'm not sure what you mean by that. How to convert data just before plotting if plotted data is of the format {{a, b}, {c, d},...} and only 1 column must be converted? $\endgroup$ – Kamila Mar 15 at 17:30
2
$\begingroup$

Like I suggested in my comment, it's probably easier to just keep the units around until you plot them. Here's a suggestion:

DynamicModule[{initQ = False},
 inputList = {{1, 5}, {2, 4}, {3, 8}};
 Dynamic[If[TrueQ[initQ], 
   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[
      Map[
       QuantityMagnitude[#, {"DimensionlessUnit", popupSelection}] &,
       plotData
       ],
      Joined -> True, ImageSize -> {300}, Frame -> True
      ]}],
   ProgressIndicator[Appearance -> "Necklace"]
   ]
  ],
 Initialization :> (popupSelection = "DegreesCelsius";
   inputData = 0;
   plotData = {};
   dataSet3 = {0};
   dataSet4 := QuantityArray[
     Transpose[{
       inputList[[All, 1]],
       dataSet3
       }],
     {"DimensionlessUnit", "DegreesCelsius"}
     ];
   initQ = True
   ),
 SynchronousInitialization -> False
]
$\endgroup$
  • $\begingroup$ It works great. Thanks for your help $\endgroup$ – Kamila Mar 15 at 21:32

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.