| Contents | Index |
validStr = validatestring(str,validStrings)
validStr = validatestring(str,validStrings,argIndex)
validStr = validatestring(str,validStrings,funcName)
validStr = validatestring(str,validStrings,funcName,varName)
validStr = validatestring(str,validStrings,funcName,varName,argIndex)
validStr = validatestring(str,validStrings) checks the validity of text string str. If str is an unambiguous, case-insensitive match to a string in cell array validStrings, the validatestring function returns the matching string in validstr. Otherwise, MATLAB throws an error and issues a formatted error message.
validStr = validatestring(str,validStrings,argIndex) includes the position of the input in your function argument list as part of any generated error messages.
validStr = validatestring(str,validStrings,funcName) includes the specified function name in generated error identifiers.
validStr = validatestring(str,validStrings,funcName,varName) includes the specified variable name in generated error messages.
validStr = validatestring(str,validStrings,funcName,varName,argIndex) includes the specified information in generated error messages or identifiers.
Check whether a string is in a set of valid values.
str = 'won';
validStrings = {'wind','wonder','when'};
validStr = validatestring(str,validStrings)Because str is a partial match to a unique string, this code returns
validStr =
wonderHowever, if str is a partial match to multiple strings, and the strings are not subsets of each other, validatestring throws an error and displays a formatted message.
str = 'won';
validStrings = {'wonder','wondrous','wonderful'};
validStr = validatestring(str,validStrings)The error message is
Expected argument to match one of these strings: wonder, wondrous, wonderful The input, 'won', matched more than one valid string.
Check inputs to a custom function, and include information about the input name and position in generated errors.
function a = findArea(shape,ht,wd,units)
a = 0;
expectedShapes = {'square','rectangle','triangle'};
expectedUnits = {'cm','m','in','ft','yds'};
shapeName = validatestring(shape,expectedShapes,mfilename,'Shape',1)
unitAbbrev = validatestring(units,expectedUnits,mfilename,'Units',4)When you call the function with valid input strings,
myarea = findArea('rect',10,3,'cm')the function stores the inputs in local variables shapeName and unitAbbrev:
shapeName =
rectangle
unitAbbrev =
cmHowever, if the inputs are not valid, such as
myarea = findArea('circle',10,3,'cm')MATLAB displays
Error using findArea Expected argument 1, Shape, to match one of these strings: square, rectangle, triangle The input, 'circle', did not match any of the valid strings.
The function name is part of the error identifier, so
MException.last.identifier
returns
MATLAB:findArea:unrecognizedStringChoice
inputParser | is* | isa | validateattributes

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |