validatestring
Matches text against a list of valid strings, allowing abbreviations.
Category: Programming > Functions
Syntax
match = validatestring(str, options)
Returns the entry of options that str matches, or errors if there is none.
Description
The comparison ignores case and accepts any leading portion of an option, so 'gr' matches 'green'. An exact match always wins over longer options that start the same way. If the text is a prefix of several options that are not prefixes of each other, the match is ambiguous and the call errors; if it matches none, the error message lists all valid values.
Returning the full spelling lets a function accept short forms from callers and still work with a canonical value in a switch.
Input arguments
str- The text to check, usually a user-supplied option.
options- Cell array of valid character vectors.
Output arguments
match- The matching entry of
options, exactly as spelled there.
Example
Abbreviations and a mismatch
s = validatestring('GR', {'red', 'green'})
try
validatestring('x', {'red', 'green'})
catch err
disp(err.message)
end
Output
s = 'green'
Expected input to match one of these values:
'red', 'green'
The input, 'x', did not match any of the valid values.