Main Content

validateLengthUnit

Validate and standardize length unit

Description

example

standardName = validateLengthUnit(unit) checks that unit is a valid length unit and converts it to a standard unit name. The function is case-insensitive with respect to its input. Spaces, periods, and apostrophes are ignored. Plural forms are accepted in most cases, but the result, standardName is always singular.

example

standardName = validateLengthUnit(unit,funcName,varName,argIndex) checks that unit is a valid length unit and, if it isn’t, creates an error message using the optional inputs funcName, varName, and argIndex in error message formatting, with behavior identical to that provided by the validateattributes inputs of the same names.

Examples

collapse all

Find the valid length unit name for 'foot' when other valid strings for 'foot' are input.

validateLengthUnit('foot')
ans = 
'foot'
validateLengthUnit('feet')
ans = 
'foot'
validateLengthUnit('international feet')
ans = 
'foot'

Find the valid length unit name for 'kilometer' when other valid strings for 'kilometer' are input.

validateLengthUnit('kilometer')
ans = 
'kilometer'
validateLengthUnit('km')
ans = 
'kilometer'
validateLengthUnit('kilometre')
ans = 
'kilometer'
validateLengthUnit('kilometers')
ans = 
'kilometer'
validateLengthUnit('kilometres')
ans = 
'kilometer'

Find the valid length unit name when the name contains an apostrophe.

validateLengthUnit('Clarke''s foot')
ans = 
'Clarke's foot'

Create custom error messages using the validateLengthUnit function. An invalid input to validateLengthUnit results in an error message referencing a function name, 'FOO', a variable name, 'UNIT' and an argument number, 5.

validateLengthUnit(17,'FOO','UNIT',5)
Error using FOO
Expected input number 5, UNIT, to be one of these types:

char, string

Instead its type was double.

Error in validateLengthUnit (line 87)
validateattributes(unit,{'char','string'},{'nonempty','scalartext'},varargin{:})

Input Arguments

collapse all

Length unit, specified as any of the following:

Unit NameValue(s)
meter'm', 'meter(s)', 'metre(s)'
centimeter'cm', 'centimeter(s)', 'centimetre(s)'
millimeter'mm', 'millimeter(s)', 'millimetre(s)'
micron'micron(s)'
kilometer'km', 'kilometer(s)', 'kilometre(s)'
nautical mile'nm', 'naut mi', 'nautical mile(s)'
foot'ft', 'international ft',
'foot', 'international foot',
'feet', 'international feet'
inch'in', 'inch', 'inches'
yard'yd', 'yds', 'yard(s)'
mile'mi', 'mile(s)',
'international mile(s)'
U.S. survey foot'sf',
'survey ft', 'US survey ft',
'U.S. survey ft', 'survey foot',
'US survey foot',
'U.S. survey foot',
'survey feet', 'US survey feet',
'U.S. survey feet'
U.S. survey mile (statute mile)'sm', 'survey mile(s)',
'statute mile(s)',
'US survey mile(s)',
'U.S. survey mile(s)'
Clarke's foot'Clarke's foot', 'Clarkes foot'
German legal metre'German legal metre',
'German legal meter'
Indian foot'Indian foot'

Example: validateLengthUnit('feet')

Data Types: char | string

Name of the function whose input you are validating, specified as a string scalar or character vector. If you specify an empty string or character vector (''), the validateLengthUnit function ignores the funcName value.

Example: validateLengthUnit(17,'FOO','UNIT',5)

Data Types: char | string

Name of input variable, specified as a string scalar or character vector. If you specify an empty string or character vector (''), the validateLengthUnit function ignores the varName value.

Example: validateLengthUnit(17,'FOO','UNIT',5)

Data Types: char | string

Position of the input argument, specified as a positive integer.

Example: validateLengthUnit(17,'FOO','UNIT',5)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Standard length unit name, returned as a character vector.

See Also

Introduced before R2006a