validatestring
Checks that text matches one allowed value.
📝Syntax
matched = validatestring(str, validStrings)
matched = validatestring(str, validStrings, argIndex)
matched = validatestring(str, validStrings, funcName)
matched = validatestring(str, validStrings, funcName, varName)
matched = validatestring(str, validStrings, funcName, varName, argIndex)
📥Input Arguments
Parameter Description
str text to validate, specified as a character vector or string scalar.
validStrings allowed text values, specified as a character vector, string array, or cell array of character vectors.
argIndex positive integer used in generated error messages to identify the argument position.
funcName function name used in generated error identifiers.
varName variable name used in generated error messages.
📤Output Arguments
Parameter Description
matched matched value from validStrings. The output is a string scalar when validStrings is a string array; otherwise it is a character vector.
📄Description

validatestring accepts exact matches and leading partial matches without case sensitivity. Exact matches are preferred over partial matches.

If one leading partial match exists, that value is returned. If several leading partial matches exist and every matching value forms a substring chain, the shortest matching value is returned. If several leading partial matches exist and they do not form such a chain, an ambiguity error is raised.

Error messages can include an argument position, a variable name, and a function name depending on the syntax used.

💡Examples
Case-insensitive exact and partial matches.
shape = validatestring('Rect', {'square', 'rectangle', 'triangle'});
direction = validatestring("LEFT", ["left", "right"])
Shortest match in a chain of partial matches.
value = validatestring('rig', {'righteously', 'right', 'righteous'})
Use context arguments for generated errors.
units = {'cm', 'm', 'in', 'ft'};
choice = validatestring('CM', units, 'findArea', 'units', 4)
🔗See Also
validateattributesinputParser
🕔Version History
Version Description
1.0.0 initial version
Edit this page on GitHub