time | Calls | line |
---|
| | 1 | function out = validatestring( varargin )
|
| | 2 | %VALIDATESTRING Check validity of text.
|
| | 3 | % VALIDSTR = VALIDATESTRING(STR,VALID_STRINGS) checks the validity of
|
| | 4 | % text STR. If STR unambiguously and case-insensitively matches the
|
| | 5 | % beginning of one of VALID_STRINGS, VALIDATESTRING returns the match
|
| | 6 | % in VALIDSTR. Otherwise, VALIDATESTRING issues a formatted error message.
|
| | 7 | %
|
| | 8 | % VALIDSTR = VALIDATESTRING(STR,VALID_STRINGS,ARG_INDEX) includes the
|
| | 9 | % position of the input in your function argument list as part of any
|
| | 10 | % generated error messages.
|
| | 11 | %
|
| | 12 | % VALIDSTR = VALIDATESTRING(STR,VALID_STRINGS,FUNC_NAME) includes the
|
| | 13 | % specified function name in generated error identifiers.
|
| | 14 | %
|
| | 15 | % VALIDSTR = VALIDATESTRING(STR,VALID_STRINGS,FUNC_NAME,VAR_NAME) includes
|
| | 16 | % the specified variable name in generated error messages.
|
| | 17 | %
|
| | 18 | % VALIDSTR = VALIDATESTRING(STR,VALID_STRINGS,FUNC_NAME,VAR_NAME,ARG_INDEX)
|
| | 19 | % includes the specified information in the generated error messages or
|
| | 20 | % identifiers.
|
| | 21 | %
|
| | 22 | % Input Arguments:
|
| | 23 | %
|
| | 24 | % VALID_STRINGS Array of strings or cell array of character vectors.
|
| | 25 | %
|
| | 26 | % ARG_INDEX Positive integer that specifies the position of the
|
| | 27 | % input argument.
|
| | 28 | %
|
| | 29 | % FUNC_NAME String scalar or character vector that specifies the
|
| | 30 | % function name. If you specify a missing string or an
|
| | 31 | % empty character vector, '', FUNC_NAME is ignored.
|
| | 32 | %
|
| | 33 | % VAR_NAME String scalar or character vector that specifies the
|
| | 34 | % input argument name. If you specify a missing string or
|
| | 35 | % an empty character vector, '', VAR_NAME is ignored.
|
| | 36 | %
|
| | 37 | % Example: Define a cell array of text strings, and pass in another
|
| | 38 | % string that is not in the cell array.
|
| | 39 | %
|
| | 40 | % validatestring('C',{'A','B'},'func_name','var_name',2)
|
| | 41 | %
|
| | 42 | % This code throws an error and displays a formatted message:
|
| | 43 | %
|
| | 44 | % Expected argument 2, var_name, to match one of these strings:
|
| | 45 | %
|
| | 46 | % 'A', 'B'
|
| | 47 | %
|
| | 48 | % The input, 'C', did not match any of the valid strings.
|
| | 49 | %
|
| | 50 | % See also validateattributes, inputParser.
|
| | 51 |
|
| | 52 | % Copyright 1993-2018 The MathWorks, Inc.
|
| | 53 |
|
< 0.001 | 20 | 54 | narginchk(2,5);
|
| | 55 |
|
< 0.001 | 20 | 56 | try
|
0.002 | 20 | 57 | [in, validStrings, optional_inputs] = checkInputs(varargin);
|
| | 58 | catch e
|
| | 59 | % only VALIDATESTRING should be on the stack
|
| | 60 | throw(e)
|
< 0.001 | 20 | 61 | end
|
| | 62 |
|
< 0.001 | 20 | 63 | try
|
| | 64 | % check the contents of IN
|
0.003 | 20 | 65 | out = checkString(in, validStrings, optional_inputs);
|
| | 66 |
|
| | 67 | catch e
|
| | 68 | myId = 'MATLAB:validatestring:';
|
| | 69 | if strncmp(myId, e.identifier, length(myId))
|
| | 70 | % leave VALIDATESTRING on the stack, because there was a misuse
|
| | 71 | % of VALIDATESTRING itself
|
| | 72 | throw(e)
|
| | 73 | else
|
| | 74 | % strip VALIDATESTRING off the stack so that the error looks like
|
| | 75 | % it comes from the caller just as if it had hand-coded its input checking
|
| | 76 | throwAsCaller( e )
|
| | 77 | end
|
< 0.001 | 20 | 78 | end
|
< 0.001 | 20 | 79 | end
|
Other subfunctions in this file are not included in this listing.