checkUnits
Checks the dimensional consistency of an expression with units.
Category: Symbolic Computations in MATLAB > Units of Measurement
Syntax
s = checkUnits(expr)
Returns a structure with the fields Consistent and Compatible.
tf = checkUnits(expr, 'Consistent')
Tests only that all added terms have exactly the same units.
tf = checkUnits(expr, 'Compatible')
Tests that added terms have convertible units.
Description
Quantities may only be added or compared if they have the same dimension. Compatible is true when every sum combines units of the same dimension, such as m and km, even if they differ in scale. Consistent is stricter and requires the very same unit in every term, so 1*m + 2*km is compatible but not consistent.
Adding a length to a time is neither: both fields are false.
Input arguments
expr- Symbolic expression with units.
Output arguments
s- Structure with logical fields.
tf- Logical result of the single requested test.
Example
Mixed, compatible and invalid sums
u = symunit;
checkUnits(1*u.m + 2*u.m, 'Consistent')
checkUnits(1*u.m + 2*u.km, 'Consistent')
s = checkUnits(1*u.m + 2*u.km)
chw = checkUnits(1*u.m + 2*u.s, 'Compatible')
Output
ans = logical
1
ans = logical
0
s = struct with fields:
Consistent: 0
Compatible: 1
chw = logical
0