how to set a unit to variable?

32 次查看(过去 30 天)
I encounter the error after run this :
30*unit::mm
  2 个评论
Walter Roberson
Walter Roberson 2020-4-20
Discussion of character strings is not very relevant to the question of whether MATLAB can handle units.

请先登录,再进行评论。

采纳的回答

Karan Gill
Karan Gill 2017-4-7
编辑:Karan Gill 2017-10-17
Units are now in MATLAB if you have Symbolic Math Toolbox. Try:
>> u = symunit;
>> x = 2*u.meter
x =
2*[m]
>> x = rewrite(x,u.ft)
x =
(2500/381)*[ft]
>> double(separateUnits(x))
ans =
6.5617
  1 个评论
Walter Roberson
Walter Roberson 2017-4-8
Note: the implementation of units has difficulty with quantity 0, which tends to make the most difference with temperature (e.g., 0C and 0F)

请先登录,再进行评论。

更多回答(2 个)

the cyclist
the cyclist 2015-12-27
In general, MATLAB variables do not have units, and what you have written is not a valid MATLAB syntax (to my knowledge).
There is a contribution to the File Exchange that allows for assigning unit to variables: http://www.mathworks.com/matlabcentral/fileexchange/29621-units-conversion-toolbox/content/UnitConversion-rev2/html/defining.html.
  1 个评论
Karan Gill
Karan Gill 2017-4-7
编辑:Karan Gill 2017-4-7
Units are now in MATLAB if you have Symbolic Math Toolbox. See the tutorial: https://www.mathworks.com/help/symbolic/units-of-measurement-tutorial.html
Posted a separate answer below so that OP can accept.

请先登录,再进行评论。


Steven Lord
Steven Lord 2015-12-27
That syntax is specifically for use in the MuPAD Notebook Interface (as stated by the note at the top of this documentation page) not in MATLAB.
  3 个评论
Walter Roberson
Walter Roberson 2020-10-13
30 * sym('unit::mm')
is no longer supported. These days you should use symunit . Or if you are despairate,
30 * feval(symengine, 'unit::mm')

请先登录,再进行评论。

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by