This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Check validity of text


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 str. If str is an unambiguous, case-insensitive match to any text in validStrings, the validatestring function returns the matching text as 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.

Input Arguments


Text to validate. str can be a character vector or a string scalar.


Text to match. validStrings can be a cell array of character vectors or a string array.


Character vector or string scalar that specifies the name of the function whose input you are validating. If you specify an empty character vector, '' or the <missing> string, the validatestring function ignores the funcname input.


Character vector or string scalar that specifies the name of the input variable. If you specify an empty character vector, '' or the <missing> string, the validatestring function ignores the varName input.


Positive integer that specifies the position of the input argument.

Output Arguments


Character vector or string scalar that contains the element of validStrings that is an unambiguous, case-insensitive match to str. The output validStr is the same type as the input str.

Example — Match 'ball' with . . .Return ValueType of Match
ball, barn, bellballExact match
balloon, barnballoonPartial match (leading characters)
ballo, balloo, balloonballo (shortest match)Multiple partial matches where each character vector is a subset of another
balloon, balletErrorMultiple partial matches to unique character vectors
barn, bellErrorNo match


Check whether a character vector 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 character vector, this code returns

validStr =


However, if str is a partial match to multiple character vectors, and the character vectors 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 input 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 character vectors,

myarea = findArea('rect',10,3,'cm')

the function stores the inputs in local variables shapeName and unitAbbrev:

shapeName =


unitAbbrev =


myarea =


However, if the inputs are not valid, such as

myarea = findArea('circle',10,3,'cm')

MATLAB displays

Error using findArea (line 6)
Expected input number 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



Was this topic helpful?