Units of measurement
u = symunit
returns the units collection. Then, specify any unit by using
u.
unit
. For example, specify
3
meters as 3*u.m
. Common alternate names
for units are supported, such as u.meter
and
u.metre
. Plurals are not supported.
Before specifying units, load units by using symunit
.
Then, specify a unit by using dot notation.
Specify a length of 3
meters. You can also use aliases
u.meter
or u.metre
.
u = symunit; length = 3*u.m
length = 3*[m]
Tip
Use tab expansion to find names of units. Type u.
, press Tab, and continue
typing.
Specify the acceleration due to gravity of 9.81
meters
per second squared. Because units are symbolic expressions, numeric
inputs are converted to exact symbolic values. Here, 9.81
is
converted to 981/100
.
g = 9.81*u.m/u.s^2
g = (981/100)*([m]/[s]^2)
If you are unfamiliar with the differences between symbolic and numeric arithmetic, see Choose Numeric or Symbolic Arithmetic.
Units behave like symbolic expressions when you perform standard operations on them. For numeric operations, separate the value from the units, substitute for any symbolic parameters, and convert the result to double.
Find the speed required to travel 5
km in 2
hours.
u = symunit; d = 5*u.km; t = 2*u.hr; s = d/t
s = (5/2)*([km]/[h])
The value 5/2
is symbolic. You may prefer double output, or require double
output for a MATLAB® function that does not accept symbolic values. Convert to double by
separating the numeric value using separateUnits
and then using
double
.
[sNum,sUnits] = separateUnits(s)
sNum = 5/2 sUnits = 1*([km]/[h])
sNum = double(sNum)
sNum = 2.5000
For the complete units workflow, see Units of Measurement Tutorial.
Use your preferred unit by rewriting units using
unitConvert
. Also, instead of specifying specific
units, you can specify that the output should be in terms of SI units.
Calculate the force required to accelerate 2
kg
by 5
m/s2. The expression
is not automatically rewritten in terms of Newtons.
u = symunit; m = 2*u.kg; a = 5*u.m/u.s^2; F = m*a
F = 10*(([kg]*[m])/[s]^2)
Convert the expression to newtons by using unitConvert
.
F = unitConvert(F,u.N)
F = 10*[N]
Convert 5
cm to inches.
length = 5*u.cm; length = unitConvert(length,u.in)
length = (250/127)*[in]
Convert length
to SI units. The result is in meters.
length = unitConvert(length,'SI')
length = (1/20)*[m]
Simplify expressions containing units of the
same dimension by using simplify
. Units are not
automatically simplified or checked for consistency unless you call simplify
.
u = symunit; expr = 300*u.cm + 40*u.inch + 2*u.m
expr = 300*[cm] + 40*[in] + 2*[m]
expr = simplify(expr)
expr = (3008/5)*[cm]
simplify
automatically chooses the unit to rewrite in terms of. To choose
a specific unit, see Convert Between Units.
By default, temperatures are assumed to represent temperature differences.
For example, 5*u.Celsius
represents a temperature difference
of 5 degrees Celsius. This assumption allows arithmetical operations on
temperature values and conversion between temperature scales.
To represent absolute temperatures, use degrees Kelvin so that you do not have to distinguish an absolute temperature from a temperature difference.
Convert 23
degrees Celsius to Kelvin, treating the temperature first as a
temperature difference and then as an absolute temperature.
u = symunit; T = 23*u.Celsius; diffK = unitConvert(T,u.K)
diffK = 23*[K]
absK = unitConvert(T,u.K,'Temperature','absolute')
absK = (5923/20)*[K]
1
represents a dimensionless unit.
Hence, isUnit(sym(1))
returns logical 1
(true
).
Certain non-linear units, such as decibels, are not implemented because arithmetic operations are not possible for these units.
Instead of using dot notation to specify units, you
can alternatively use string input as symunit(unit)
.
For example, symunit('m')
specifies the unit meter.
checkUnits
| isUnit
| newUnit
| separateUnits
| symunit2str
| unitConversionFactor
| unitConvert
| unitInfo