Message Boards Message Boards

GROUPS:

Why doesn't Quantity [ ] accept a Dynamic expression as a second argument?

Posted 7 years ago
8436 Views
|
2 Replies
|
0 Total Likes
|

If anyone can help me with this, it would be greatly appreciated. I'm using the following code with the intent of using controls to easily input values and units:

Slider[Dynamic[b]]
SetterBar[
 Dynamic[lengthUnit], {"feet", "inches", "centimeters", "meters"}]
Quantity[Dynamic[b], Dynamic[lengthUnit]]

The above code returns "Quantity[0.,feet]"

But if I leave Dynamic out of the second argument by substituting the following line, everything works as expected:

Quantity[Dynamic[b],lengthUnit]

I suspect that this has something to do with the HoldRest attribute of Quantity and the HoldFirst attribute of Dynamic, but I'm not sure. Does anyone have any suggestions of how to make this work?

Thank you

2 Replies

Units such as "feet" need to be capitalized so Mathematica will recognize that they are actually Units. Also, if you want the Quantity command to update dynamically then you need wrap Dynamic around it. Try this for the last line:

Dynamic[Quantity[b,lengthUnit]]

Here is more information about Quantity[] and Units .

Hope that works for you.

Posted 7 years ago

Thank you for the quick post Kurt - that totally did the trick. I'm still learning my way around Dynamic so this helps a lot. Thanks also for the capitalization tip with Quantity.

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