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.

separateUnits

Separate units from expression

Syntax

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

Description

example

[Data,Units] = separateUnits(expr) returns the 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

Separate Units and Expression

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 =
1*([m]/[s])

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

Data = separateUnits(speed)
Data =
10*t

Separate Incompatible Units

When the expression has incompatible units, separateUnits errors. Units are incompatible when they do not have the same dimensions, such as length or time.

Separate the units from 2*u.m + 3*u.s, where u = symunit. The separateUnits function throws an error. Instead, to list the units in the input, use findUnits.

u = symunit;
[Data,Units] = separateUnits(2*u.m + 3*u.s)
Error using separateUnits (line 51)
Input has incompatible units.

Separate Inconsistent Units

When the input has inconsistent units that can be converted to the same unit, then separateUnits performs the conversion and returns the separated result. Units are inconsistent when they cannot be converted to each other with a conversion factor of 1

Separate the units from 2*u.m + 30*u.cm. 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)
Data =
230
Units =
[cm]

Input Arguments

collapse all

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

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, specified as symbolic units.

Introduced in R2017a