Main Content

symunit

Units of measurement

Description

example

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. For details about the list of available units, see Units and Unit Systems List.

Examples

collapse all

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 = 3m"meter - a physical unit of length."

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 = 

981100m"meter - a physical unit of length."s"second - a physical unit of time."2

For more information about 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 = 

52km"kilometer - a physical unit of length."h"hour - a physical unit of time."

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 = 

52

sUnits = 

km"kilometer - a physical unit of length."h"hour - a physical unit of time."

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/s². 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 = 

10kg"kilogram - a physical unit of mass."m"meter - a physical unit of length."s"second - a physical unit of time."2

Convert the expression to newtons by using unitConvert.

F = unitConvert(F,u.N)
F = 10N"newton - a physical unit of force."

Convert 5 cm to inches.

length = 5*u.cm;
length = unitConvert(length,u.in)
length = 

250127in"inch - a physical unit of length."

Convert length to SI units. The result is in meters.

length = unitConvert(length,'SI')
length = 

120m"meter - a physical unit of length."

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 = 300cm"centimeter - a physical unit of length."+40in"inch - a physical unit of length."+2m"meter - a physical unit of length."
expr = simplify(expr)
expr = 

752125m"meter - a physical unit of length."

simplify automatically chooses the unit to return. To convert to a specific unit, use unitConvert.

expr = unitConvert(expr,u.ft)
expr = 

7520381ft"foot - a physical unit of length."

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 K, 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 = 23K"kelvin - a physical unit of temperature."
absK = unitConvert(T,u.K,'Temperature','absolute')
absK = 

592320K"kelvin - a physical unit of temperature."

Limitations

  • When using symbolic units, the value of 0 times a symbolic unit is returned as a dimensionless 0. To preserve the unit when multiplying a symbolic unit by 0, use a cell array to represent the zero measurement.

    For example, you can define 0 degrees Celsius as a cell array and convert it to degrees Fahrenheit by using the unitConvert function.

    u = symunit;
    tC = {0,u.Celsius};
    tF = unitConvert(tC,u.Fahrenheit,'Temperature','Absolute')
    tF =
    32*[Fahrenheit]

Tips

  • You can use tab expansion to find names of units. Type u., press Tab, and continue typing.

  • 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 for symunit(unit). For example, symunit("m") specifies the unit meter.

Version History

Introduced in R2017a

expand all