6

Is there a function in Octave that returns the position of the first occurrence of a string in a cell array?

I found findstr but this returns a vector, which I do not want. I want what index does but it only works for strings.

If there is no such function, are there any tips on how to go about it?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
CH123
  • 251
  • 1
  • 5
  • 15
  • Do you want the first occurrence in each cell or for the entire cell array? – sco1 Apr 28 '16 at 15:02
  • Reopened from the duplicate because this question is asking to find the **first occurrence** of the string, not **all occurrences**. The accepted answer also handles the nuance of not being able to find the string. This question has a fundamental difference which leads to a different solution. – rayryeng Oct 25 '17 at 18:40

1 Answers1

8

As findstr is being deprecated, a combination of find and strcmpi may prove useful. strcmpi compares strings by ignoring the case of the letters which may be useful for your purposes. If this is not what you want, use the function without the trailing i, so strcmp. The input into strcmpi or strcmp are the string to search for str and for your case the additional input parameter is a cell array A of strings to search in. 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 cell array A matched with str. You would then use find to find all locations of where the string matched, but you can further restrain it by specifying the maximum number of locations n as well as where to constrain your search - specifically if you want to look at the first or last n locations where the string matched.

If the desired string is in str and your cell array is stored in A, simply do:

index = find(strcmpi(str, A)), 1, 'first');

To reiterate, find will find all locations where the string matched, while the second and third parameters tell you to only return the first index of the result. Specifically, this will return the first occurrence of the desired searched string, or the empty array if it can't be found.

Example Run

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)
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 2
    Worth noting that `strcmp`/`strcmpi` only compares *full* strings, so if you're searching for the existence substring you'll probably need to use a regex. – sco1 Apr 28 '16 at 15:13
  • @excaza Good comment. I assumed the OP wanted to search for the full string. The language suggests so, but my assumptions have bitten me in the @$$ before. – rayryeng Apr 28 '16 at 15:13
  • 1
    @rayryeng This is perfect! Thank you. And you're assumption was right, I wanted to search for the full string. – CH123 Apr 28 '16 at 15:45
  • 1
    @CH123 Ah very nice, then the above code will be very suitable for your purposes. Good luck! – rayryeng Apr 28 '16 at 15:47
  • @rayryeng, Thank you for your nice answer. However I couldn't find any comment of [excaza](https://stackoverflow.com/users/2748311/excaza) above your answer. May be it is deleted. Would you mind editing your answer accordingly? – Md. Abu Nafee Ibna Zahid Feb 06 '18 at 12:31
  • @Md.AbuNafeeIbnaZahid it was a comment and I unfortunately can't remember what he said. – rayryeng Feb 06 '18 at 12:49