# unitConvert

Convert units to other units of measurement

## 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 =

$\frac{250}{127} \mathrm{in}\mathrm{"inch - a physical unit of length."}$

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 \mathrm{in}\mathrm{"inch - a physical unit of length."}$

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

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

m = 2*u.kg;
a = 5*u.m/u.s^2;
F = m*a
F =

$10 \frac{\mathrm{kg}\mathrm{"kilogram - a physical unit of mass."} \mathrm{m}\mathrm{"meter - a physical unit of length."}}{{\mathrm{s}\mathrm{"second - a physical unit of time."}}^{2}}$

Convert F to newtons by using unitConvert.

F = unitConvert(F,u.N)
F = $10 \mathrm{N}\mathrm{"newton - a physical unit of force."}$

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 =

$\frac{25}{18} \frac{\mathrm{m}\mathrm{"meter - a physical unit of length."}}{\mathrm{s}\mathrm{"second - a physical unit of time."}}$

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 =

$\frac{25000}{3} \frac{\mathrm{cm}\mathrm{"centimeter - a physical unit of length."}}{\mathrm{min}\mathrm{"minute - a physical unit of time."}}$

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 =

$\frac{6250}{381} \mathrm{ft}\mathrm{"foot - a physical unit of length."}$

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 \mathrm{dyn}\mathrm{"dyne - a physical unit of force."}$
cgsBase = unitConvert(F,'CGS')
cgsBase =

$1000000 \frac{\mathrm{cm}\mathrm{"centimeter - a physical unit of length."} \mathrm{g}\mathrm{"gram - a physical unit of mass."}}{{\mathrm{s}\mathrm{"second - a physical unit of time."}}^{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 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 = $23 \mathrm{K}\mathrm{"kelvin - a physical unit of temperature."}$
absK = unitConvert(T,u.K,'Temperature','absolute')
absK =

$\frac{5923}{20} \mathrm{K}\mathrm{"kelvin - a physical unit of temperature."}$

## Input Arguments

collapse all

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

Units to convert input to, specified as a symbolic unit or vector 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'.

## 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]

## Version History

Introduced in R2018b