Skip to content

validatestring

strings · scripts/strings/validatestring.m

: validstr = validatestring (str, strarray)
: validstr = validatestring (str, strarray, funcname)
: validstr = validatestring (str, strarray, funcname, varname)
: validstr = validatestring (…, position)

Match a possibly abbreviated option against a list of allowed values.

str is compared, ignoring case, with the start of every entry of the cell array of strings strarray. The entry that matches is returned in its full, canonical spelling, so "r" can stand for "red". When several entries match, the match is accepted only if one of them is a prefix of all the others, in which case that shortest entry is returned; otherwise the abbreviation is ambiguous and an error is raised. No match at all is also an error.

funcname, varname and position are used only to word the error message: the name of the calling function, the name of the argument being checked and its position in the argument list.

validatestring ("gr", {"red", "green", "blue"})
⇒  "green"

validatestring ("b", {"red", "green", "blue", "black"})
⇒  error: the abbreviation matches both "blue" and "black"