As findstr deprecated, a combination of find and strcmpi may be useful. strcmpi compares strings, ignoring the case of letters that may be useful for your purposes. If this is not what you want, use a function without a finite i , so strcmp . The input to strcmpi or strcmp is the string to search for str , and for your case, the additional input parameter is an array of A cells of the lines to search. The output of strcmpi or strcmp will give you a vector of logical values, where each location k tells you whether the string k in the array of cells A str . Then you would use find to find all the locations where the string matches, but you can further limit it by specifying the maximum number of places n , and also where you can limit your search - especially if you want to look at the first or last n locations where the string matches.
If the desired row is in str and your array of cells is stored in A , just do:
index = find(strcmpi(str, A)), 1, 'first');
To repeat, find will find all the locations where the string matches, and the second and third parameters tell you to return only the first index of the result. In particular, this will return the first occurrence of the search string or an empty array if it cannot be found.
Run example
octave:8> A = {'hello', 'hello', 'how', 'how', 'are', 'you'}; octave:9> str = 'hello'; octave:10> index = find(strcmpi(str, A), 1, 'first') index = 1 octave:11> str = 'goodbye'; octave:12> index = find(strcmpi(str, A), 1, 'first') index = [](1x0)
source share