3
$\begingroup$

Trying to understand work of InputField in Manipulate. My example code is:

Manipulate[ListPlot[(ToExpression@list), 
PlotRange -> {{Automatic, max}, Automatic}], 
Control[{{list, "Range[10]", "List"}, 
InputField[#, String] &}], {{max, 10, "Max range"}, InputField}]

So, I have some list (here just Range[10] for simplicity), which can be entered by user in corresponding InputField and I want to control plotting range (max variable here). Now if I change Range[...] the plot range doesn't change, I suppose max should somehow become equal to Max@ToExpression@list. How to organize this?

Thanks.

$\endgroup$
1
$\begingroup$

As I understand the question, the OP would like the user to be able to control the upper limit of the horizontal axis; and would like max to be set to the maximum range whenever list is changed, after which the user can change max as desired. In that case, a typical approach is to use TrackingFunction to set the other variables. I thought it would be more efficient to have another hidden variable (no control) to be the actual list, since its value is needed in a couple of places.

Manipulate[
 ListPlot[list, PlotRange -> {{Automatic, max}, Automatic}],
 Control[{{str, "Range[10]", "List"},
   InputField[#, String] &,
   TrackingFunction -> (
     (str = #;
       list = ToExpression[str];
       (* Optional: add check here, set flag if not a List *)
       max = Length[list]) &)}],
 {{max, 10, "Max range"}, InputField},
 {{list, Range[10]}, ControlType -> None}
 ]
$\endgroup$
  • $\begingroup$ Yes, this is exactly what I need, many thanks Michael! $\endgroup$ – Alx Jul 8 at 1:36
  • $\begingroup$ @Sam_1q You're welcome! $\endgroup$ – Michael E2 Jul 8 at 3:19

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.