How do I see the index values of strfind ?

10 views (last 30 days)
j smith
j smith on 25 Nov 2015
Commented: j smith on 25 Nov 2015
I am trying to use strfind to locate values in a cell array and then compare the adjacent cells to a string that I have in order to see if the string matches the array. Ex.
A={'a' 'c' 'a' 't' 'e' 'f' 'g'}
B=strfind(A,'a')
  2 Comments
Guillaume
Guillaume on 25 Nov 2015
A=['a' 'c' 'a' 't' 'e' 'f' 'g']
is not a cell array. It is an array of character, basically a string and the above line is the same as
A = 'acatefg'
So, have you got a cell array of single characters
A = {'a' 'c' 'a' 't' 'e' 'f' 'g'}
a string as you've shown in your example, or something else?
j smith
j smith on 25 Nov 2015
I have a cell array of single characters like you showed in the third example

Sign in to comment.

Answers (2)

Guillaume
Guillaume on 25 Nov 2015
If you do have a cell array, unlike your example (see comment to your question), and you're trying to match a string in the cell array, then it is not strfind you need but strcmp:
A = {'a' 'c' 'a' 't' 'e' 'f' 'g'}
find(strcmp(A, 'a'))

Star Strider
Star Strider on 25 Nov 2015
You can’t find 'b' in your ‘A’ array because there is no 'b' in ‘A’.
Please describe in a bit more detail what you want to do.
  3 Comments
Star Strider
Star Strider on 25 Nov 2015
This is not robust, but will allow you to find the indices of the two (or fewer) characters next to the character-of-interest. If there are fewer than two, it includes the index of your variable-of-interest, but that is likely unavoidable:
A=['a' 'c' 'a' 't' 'e' 'f' 'g']
B=strfind(A,'a')
Bnext = [max(1,B(1)-1), min(B(1)+1,length(A)); max(B(1),B(2)-1), min(B(2)+1,length(A))];
Bnext =
1 2
2 4
You can build on it to make it more robust to fit your needs.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!