separateUnits

Separate units from expression

Syntax

``````[Data,Units] = separateUnits(expr)``````
``Data = separateUnits(expr)``

Description

example

``````[Data,Units] = separateUnits(expr)``` returns the symbolic units of the symbolic expression `expr` in `Units` and the rest of `expr` in `Data`.```

example

````Data = separateUnits(expr)` removes symbolic units from `expr` and then returns the rest.```

Examples

collapse all

Separate the units from the expression `10*t*u.m/u.s`, where `u = symunit`, by providing two output arguments for `separateUnits`.

```u = symunit; syms t speed = 10*t*u.m/u.s; [Data,Units] = separateUnits(speed)```
`Data = $10 t$`
```Units =  $\frac{\mathrm{m}\mathrm{"meter - a physical unit of length."}}{\mathrm{s}\mathrm{"second - a physical unit of time."}}$```

Return only the expression with the units removed by providing one output argument.

`Data = separateUnits(speed)`
`Data = $10 t$`

If the input has compatible units that can be converted to the same unit, then `separateUnits` performs the conversion and returns the separated result. Units are compatible when they have the same dimensions (although the conversion factor may not necessarily be 1).

Separate the units from `2*u.m + 30*u.cm + 12*u.in`. Even though the units differ, `separateUnits` converts them to the same unit and returns the separated result.

```u = symunit; [Data,Units] = separateUnits(2*u.m + 30*u.cm + 12*u.in)```
```Data =  $\frac{1628}{625}$```
`Units = $\mathrm{m}\mathrm{"meter - a physical unit of length."}$`

Input Arguments

collapse all

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

When adding or subtracting quantities with different units in `expr`, the units must be compatible. Units are compatible when they have the same dimensions. For example, see Separate Compatible Units.

Output Arguments

collapse all

Expression after removing units, returned as a number, vector, matrix, or multidimensional array, or a symbolic number, variable, vector, matrix, equation, multidimensional array, function, or expression.

Units from input, returned as symbolic units.

Tips

• If an expression has incompatible units, then `separateUnits` errors. Units are incompatible when they do not have the same dimensions, such as length and time.

For example, this code will error.

```u = symunit; [Data,Units] = separateUnits(2*u.m + 3*u.s)```

Instead, to return the units in the input, use `findUnits`.

`Units = findUnits(2*u.m + 3*u.s)`
```Units = [[m], [s]]```

Version History

Introduced in R2017a

expand all