time | calls | line |
---|
| | 82 | function out = checkString( in, validStrings, optional_inputs )
|
| | 83 |
|
| 6 | 84 | try
|
| 6 | 85 | if ~(ischar(in) && strcmp(in,''))
|
| 6 | 86 | validateattributes( in, {'char'}, {'row'} );
|
| 6 | 87 | end
|
| | 88 | catch e
|
| | 89 | [ fname, msgId, argname, argpos ] = matlab.internal.validators.generateArgumentDescriptor( ...
|
| | 90 | optional_inputs, 'validatestring' );
|
| | 91 | argDes = matlab.internal.validators.getArgumentDescriptor( msgId, argname, argpos );
|
| | 92 | me = MException( matlab.internal.validators.generateId( fname, 'unrecognizedStringChoice' ),...
|
| | 93 | '%s', getString(message( 'MATLAB:validatestring:unrecognizedStringChoice2', ...
|
| | 94 | argDes, validStringList( validStrings ))));
|
| | 95 | me = me.addCause( e );
|
| | 96 | throwAsCaller(me);
|
| | 97 | end
|
| | 98 |
|
| | 99 | % do a case insensitive search, but use the case from validStrings,
|
| | 100 | % not the case from the input
|
| 6 | 101 | if isempty(in)
|
| | 102 | out = validStrings( ismember(validStrings,in) );
|
| 6 | 103 | else
|
| 6 | 104 | out = validStrings( strncmpi( in, validStrings, length(in) ) );
|
| 6 | 105 | end
|
| | 106 |
|
| 6 | 107 | if numel( out ) == 1
|
| | 108 | % unambiguous match
|
| 6 | 109 | out = out{1};
|
| | 110 | elseif numel( out ) > 1
|
| | 111 | % possibly ambiguous match
|
| | 112 |
|
| | 113 | % determine if all the matching strings are substrings of each other
|
| | 114 | [ shortestMatchLength, shortestMatchIdx] = min( cellfun( @length, out ) );
|
| | 115 | shortestMatch = out{shortestMatchIdx};
|
| | 116 | allSubstrings = all( strncmpi( shortestMatch, out, shortestMatchLength ) );
|
| | 117 |
|
| | 118 | if allSubstrings
|
| | 119 | % return the shortest match
|
| | 120 | out = shortestMatch;
|
| | 121 | else
|
| | 122 | [ fname, msgId, argname, argpos ] = matlab.internal.validators.generateArgumentDescriptor( ...
|
| | 123 | optional_inputs, 'validatestring' );
|
| | 124 | argDes = matlab.internal.validators.getArgumentDescriptor( msgId, argname, argpos );
|
| | 125 |
|
| | 126 | error( matlab.internal.validators.generateId( fname, 'ambiguousStringChoice' ), '%s', ...
|
| | 127 | getString(message('MATLAB:validatestring:ambiguousStringChoice', ...
|
| | 128 | argDes, validStringList( validStrings ), in)))
|
| | 129 | end
|
| | 130 | else
|
| | 131 | % no match found
|
| | 132 | [ fname, msgId, argname, argpos ] = matlab.internal.validators.generateArgumentDescriptor( ...
|
| | 133 | optional_inputs, 'validatestring' );
|
| | 134 | argDes = matlab.internal.validators.getArgumentDescriptor( msgId, argname, argpos );
|
| | 135 |
|
| | 136 | error( matlab.internal.validators.generateId( fname, 'unrecognizedStringChoice' ), '%s', ...
|
| | 137 | getString(message('MATLAB:validatestring:unrecognizedStringChoice3', ...
|
| | 138 | argDes, validStringList( validStrings ), in )));
|
| | 139 | end
|
| | 140 |
|
| 6 | 141 | end
|
Other subfunctions in this file are not included in this listing.