When using a quantity interpreter on a unit without magnitude, the interpreter fails, but works fine on quantities with magnitudes.




Interpreter["Quantity"]["1 meter"]

Out: 1m, as expected.

What I expect:


Out: m

I don't see why the interpreter won't do this. The only solution I can find for when I have a LIST of magnitude-less quantities is

Interpreter["Quantity"][StringJoin["1 ", #] & /@ table]

But that's a silly workaround, and then I can't remove the 1.


What I want: Given a list of (string) units, returns a list of those units in a computational format.

The problem: Using Interpreter["Quantity"]["Unit"] FAILS, while Interpreter["Quantity"]["1 Unit"] works. However, I just want to return "Unit", not "1 Unit".

P.S For anyone interested in my application, I need a list of all units in WL for a CompatibleUnit program I'm making.

So I have

table = Keys[QuantityUnits`Private`$UnitReplacementRules]
  • $\begingroup$ I am not sure that I understand, however, would Quantity["meter"] do what you want? Its FullForm is Quantity[None, "Meters"]. Notice however that "meter" is NOT present as a string in the table you generate at the end of your post; "Meters" can be found in there instead. $\endgroup$ – MarcoB Apr 27 '18 at 21:05
  • $\begingroup$ Not sure if I understand either, so not sure if this does what you want, but Interpreter[Quantity["meter"]][[1, 2]] gives Meters, and the latter is in a computational format. E.g., if you set ans = Interpreter[Quantity["meter"]][[1, 2]] , then evaluate UnitConvert[ans, "Feet"], you'll get 1250/381 ft $\endgroup$ – theorist Apr 27 '18 at 21:28
  • $\begingroup$ @MarcoB, it's not quite what I want, because I want to be able to extend it to when the user doesn't input the exactly correct name; that's why I want to use interpreter. Note that Interpreter["Quantity"]["meter"] fails. I don't mind what the FullForm output is. I just want `Interpreter["Quantity"]["Unit"] to return unit. However, it fails $\endgroup$ – Max Coplan Apr 28 '18 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.