strmatch
is not
recommended. Use strncmp
instead.
To find exact matches for a character vector, use strcmp
. For example, if
your code currently contains lines such as these:
list = {'max','minimax','maximum','max'}
x = strmatch('max',list,'exact')
Then, MATLAB returns the following to indicate that the first and fourth array
elements are the only exact matches for max
:
x =
1
4
Replace the original code with this:
list = {'max','minimax','maximum','max'}
x = strcmp(list,'max')
MATLAB now returns the following logical array. MATLAB returns
true
(logical 1
) to indicate that the
first and fourth list
elements are max
and
false
(logical 0
) to indicate that the
second and third list
elements are not
max
.
x =
1 0 0 1
If you prefer for MATLAB to return the numeric indices of
list
, use find
, as follows:
list = {'max','minimax','maximum','max'}
x = find(strcmp(list,'max'))
Now, MATLAB returns the following to indicate that the first and fourth array
elements are the only exact matches for max
:
x =
1 4
To find values that contain a given character vector, rather than those that
are an exact match, use strncmp
.