Main Content

validateLengthUnit

Validate and standardize length unit

Description

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.

example

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:

ValueUnit Name
"m", "meter", "meters", "metre", "metres"Meters
"cm", "centimeter", "centimeters", "centimetre", "centimetres"Centimeters
"mm", "millimeter", "millimeters", "millimetre", "millimetres"Millimeters
"micron", "microns"Microns
"km", "kilometer", "kilometers", "kilometre", "kilometers"Kilometers
"nm", "naut mi", "nautical mile", "nautical miles"Nautical miles
"ft", "international ft", "foot", "international foot", "feet", "international feet"Feet
"in", "inch", "inches"Inches
"yd", "yds", "yard", "yards"Yards
"mi", "mile", "miles", "international mile", "international miles"Miles
"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 feet
"sm", "survey mile", "survey miles", "statute mile", "statute miles", "US survey mile", "US survey miles", "U.S. survey mile(s)", "U.S. survey miles"U.S. survey miles (statute miles)
"Clarke's foot", "Clarkes foot"Clarke's feet
"German legal metre", "German legal meter"German legal metres
"Indian foot"Indian feet

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.

Version History

Introduced before R2006a

See Also