time | Calls | line |
---|
| | 81 | function out = checkString( in, validStrings, optional_inputs )
|
| | 82 |
|
< 0.001 | 20 | 83 | if isstring(validStrings) && any(ismissing(validStrings))
|
| | 84 | throwError(optional_inputs, 'missingValueStringList', 'unrecognizedStringChoice');
|
< 0.001 | 20 | 85 | end
|
| | 86 |
|
< 0.001 | 20 | 87 | try
|
< 0.001 | 20 | 88 | if ~(ischar(in) && strcmp(in,''))
|
< 0.001 | 20 | 89 | validateattributes(in, {'char', 'string'}, {'scalartext'});
|
< 0.001 | 20 | 90 | end
|
| | 91 | catch e
|
| | 92 | me = createValidateattributesException(validStrings, optional_inputs,e);
|
| | 93 | throwAsCaller(me);
|
< 0.001 | 20 | 94 | end
|
| | 95 |
|
| | 96 | % do a case insensitive search, but use the case from validStrings,
|
| | 97 | % not the case from the input
|
| | 98 |
|
< 0.001 | 20 | 99 | if isstring(in) && ismissing(in)
|
| | 100 | out={};
|
< 0.001 | 20 | 101 | else
|
< 0.001 | 20 | 102 | in_char = char(in);
|
< 0.001 | 20 | 103 | if isempty(in_char)
|
| | 104 | out = validStrings(ismember(validStrings,in_char));
|
< 0.001 | 20 | 105 | else
|
< 0.001 | 20 | 106 | out = validStrings(strncmpi(in_char, validStrings, numel(in_char)));
|
< 0.001 | 20 | 107 | end
|
< 0.001 | 20 | 108 | end
|
| | 109 |
|
< 0.001 | 20 | 110 | if isscalar(out)
|
< 0.001 | 20 | 111 | if iscell(out)
|
< 0.001 | 20 | 112 | out = out{1};
|
< 0.001 | 20 | 113 | end
|
| | 114 | elseif numel(out) > 1
|
| | 115 | % possibly ambiguous match
|
| | 116 |
|
| | 117 | % determine if all the matching strings are substrings of each other
|
| | 118 | [shortestMatchLength, shortestMatchIdx] = min(cellfun('length', cellstr(out)));
|
| | 119 |
|
| | 120 | shortestMatch = out{shortestMatchIdx};
|
| | 121 | allSubstrings = all(strncmpi(shortestMatch, out, shortestMatchLength));
|
| | 122 |
|
| | 123 | if allSubstrings
|
| | 124 | % return the shortest match
|
| | 125 | if iscellstr(out)
|
| | 126 | out = shortestMatch;
|
| | 127 | else
|
| | 128 | out = string(shortestMatch);
|
| | 129 | end
|
| | 130 | else
|
| | 131 | throwErrorWithArgDescriptor(in, validStrings, optional_inputs, 'ambiguousStringChoice', 'ambiguousStringChoice');
|
| | 132 | end
|
| | 133 | else
|
| | 134 | if ismissing(in)
|
| | 135 | throwError(optional_inputs, 'missingValueInputString', 'unrecognizedStringChoice');
|
| | 136 | else
|
| | 137 | throwErrorWithArgDescriptor(in, validStrings, optional_inputs, 'unrecognizedStringChoice3', 'unrecognizedStringChoice');
|
| | 138 | end
|
< 0.001 | 20 | 139 | end
|
< 0.001 | 20 | 140 | end
|
Other subfunctions in this file are not included in this listing.