This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

unitConvert

Convert units to other units of measurement

Syntax

unitConvert(expr,units)
unitConvert(expr,unitSystem)
unitConvert(expr,unitSystem,'Derived')
___ = unitConvert(___,'Temperature',convMode)

Description

example

unitConvert(expr,units) converts symbolic units in the expression expr to the units units, where units can be a compound unit or a vector of units.

example

unitConvert(expr,unitSystem) converts expr to the unit system unitSystem. By default, the SI, CGS, and US unit systems are available. You can also define custom unit systems by using newUnitSystem.

unitConvert(expr,unitSystem,'Derived') converts units to derived units of unitSystem.

example

___ = unitConvert(___,'Temperature',convMode) indicates whether temperatures represent absolute temperatures or temperature differences by specifying 'absolute' or 'difference' respectively, using input arguments in the previous syntaxes. The 'Temperature' argument affects only conversion between units of temperature. By default, temperatures are assumed to be differences.

Examples

collapse all

Convert 5 cm to inches. Because the calculation is symbolic, unitConvert returns a symbolic fractional result.

u = symunit;
length = unitConvert(5*u.cm,u.in)
length =
(250/127)*[in]

If conversion is not possible, unitConvert returns the input.

Convert length to floating point by separating the value using separateUnits and converting using double. Alternatively, keep the units by using vpa instead of double.

double(separateUnits(length))
ans =
    1.9685
vpa(length)
ans =
1.968503937007874015748031496063*[in]

For more complex workflows, see Unit Conversions and Unit Systems.

Calculate the force required to accelerate 2 kg by 5 m/s2. The result is not automatically in newtons.

m = 2*u.kg;
a = 5*u.m/u.s^2;
F = m*a
F =
10*(([kg]*[m])/[s]^2)

Convert F to newtons by using unitConvert.

F = unitConvert(F,u.N)
F =
10*[N]

Convert 5 km per hour to meters per second by specifying meters per second as a compound unit.

u = symunit;
unitConvert(5*u.km/u.hr,u.m/u.s)
ans =
(25/18)*([m]/[s])

Specify multiple units for conversion by specifying the second argument as a vector of units. This syntax lets you specify units for every dimension to get the desired units.

Convert 5 km per hour to centimeters per minute.

u = symunit;
f = 5*u.km/u.hr;
units = [u.cm u.min];
unitConvert(f,units)
ans =
(25000/3)*([cm]/[min])

Instead of converting to specific units, you can convert to units of a unit system, such as SI, CGS, or US.

Convert 5 meters to the 'US' unit system. unitConvert returns the result in feet.

u = symunit;
unitConvert(5*u.m,'US')
ans =
(6250/381)*[ft]

Convert 10 newtons to derived units in CGS by using the input 'Derived'. The result is in dynes. Repeat the conversion without the input 'Derived' to get a result in base units.

F = 10*u.N;
cgsDerived = unitConvert(F,'CGS','Derived')
cgsDerived =
1000000*[dyn]
cgsBase = unitConvert(F,'CGS')
cgsBase =
1000000*(([cm]*[g])/[s]^2)

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]

Input Arguments

collapse all

Input, specified as a symbolic number, variable, expression, function, or array.

Units to convert input to, specified as a symbolic unit or an array of symbolic units.

Unit system to convert input to, specified as a string or character vector. By default, the SI, CGS, and US unit systems are available. You can also define custom unit systems. See Unit Conversions and Unit Systems.

Temperature conversion mode, specified as 'difference' or 'absolute'.

Introduced in R2018b