How do I see the index values of strfind ?

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

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?
I have a cell array of single characters like you showed in the third example

Sign in to comment.

Answers (2)

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'))
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

what I meant to say was I want to find 'a' in this array and then be able to find the where in the array this value is so that I can compare it to the other characters next to it.
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.
I'll try this out

Sign in to comment.

Categories

Asked:

on 25 Nov 2015

Commented:

on 25 Nov 2015

Community Treasure Hunt

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

Start Hunting!