MATLAB: Does STRMATCH in MATLAB cause an error for a cell array containing a numeric value

celldoubleMATLABstrmatch

Why does STRMATCH in MATLAB cause an error for a cell array containing a numeric value?
According to the help for STRMATCH:
I = STRMATCH(STR,STRS) looks through the rows of the character array or cell array of strings
STRS to find strings that begin with string STR, returning the matching row indices.
STRMATCH is fastest when STRS is a character array.
However, as the following example illustrates, STR can be a numeric value, and STRMATCH will not return an error.
str = 9;
strs = strvcat('This is a string array','with two rows');
ind = strmatch(str,strs);
The variable 'ind' will be empty. However, if I try to use STRMATCH with a cell array containing a double, I receive an error as in this next example:
str ={9};
strs = strvcat('This is a cell array of strings','with two rows');
ind = strmatch(str,strs);
??? Error using ==> cell/strmatch
Requires character array or cell array of strings as inputs.

Best Answer

In this example:
str = 9;
strs = strvcat('This is a string array','with two rows');
ind = strmatch(str,strs);
the normal STRMATCH is used, since the first input is not a cell array. In this case, 9 could be used as an ASCII value, and converted to a tab character. That character can then be compared with the other strings.
In this example:
str ={9};
strs = strvcat('This is a cell array of strings','with two rows');
ind = strmatch(str,strs);
@CELL/STRMATCH will be used, since the first input is a cell array. In this case, the code checks that the cell array contains strings, and converts it to a char array:
if iscellstr(str), str = char(str); end
Cell arrays of strings are special cases of cell arrays, which are commonly used to store strings together which have different lengths, and there are many instances where cell arrays of strings and strings can be used interchangeably. However, {9} is not a cell array of strings, so it is not converted to a character array, which leads to the error being thrown by this code:
if ~ischar(str) | ~ischar(strs)
error('Requires character array or cell array of strings as inputs.')
end