2
$\begingroup$

Suppose I have something like

Manipulate[Plot[Sin[n x], {x, -Pi, Pi}], {n, 20, "Frequency"}]

with a manual input field but I want to accept inputs like 20 Hz. Is there any way of doing that?

$\endgroup$

1 Answer 1

2
$\begingroup$

Does it fit your needs?

Manipulate[

  Plot[Sin[hz x], {x, -Pi, Pi}]

, { {input, "1 Hz", "Frequency"}
  , InputField[#, String, ##2] &
  , TrackingFunction :> Function[
      new
    , Catch[
        hz = Check[
          QuantityMagnitude[Interpreter["Quantity"] @ new, "Hertz"]
        , Throw @ $Failed
        ]
      ; input = new
      ]
    ]
  }
, {{hz, 1}, None} (*just for scoping*)
]

enter image description here

$\endgroup$
3
  • $\begingroup$ Actually, whoever edited the question screwed up. My original question was about % input, so if you input 20% it should be passed on as 0.2 (so, you want to transform the input in a fixed way). $\endgroup$
    – Igor Rivin
    Commented Oct 22, 2019 at 16:53
  • $\begingroup$ But looks like the right thing, thanks! $\endgroup$
    – Igor Rivin
    Commented Oct 22, 2019 at 16:56
  • $\begingroup$ @IgorRivin it was me because it was slightly confusing. If you want we can revert it and add clarification. $\endgroup$
    – Kuba
    Commented Oct 23, 2019 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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