Find Which Column contains a pattern

2 views (last 30 days)
Mary
Mary on 8 May 2014
Edited: dpb on 8 May 2014
Hello!
I have a very large cell array in which I would like to find which column (by column number) contains a pattern so that I can use that column number later as a variable
An example of my cell array would be as follows:
survey = {'A','B','CDE','FGH','IJK.:LMNO','PQR.:ST'}
I would like to know that pattern '.:' starts in survey{5}. So I'm really looking for the number 5 as an output.
I've tried strfind and regexp which give me
ans = {[],[],[],[],[4],[4]}
From here how would I get an output that locates the column of the first non-zero number?
Code:
survey = {'A','B','CDE','FGH','IJK.:LMNO','PQR.:ST'};
quest = regexp(survey,'.:','start');
location = find (quest >1) % this is where I need help...
location = 5
survey{location} = 'IJK.:LMNO'

Accepted Answer

dpb
dpb on 8 May 2014
>> loc=find(cellfun(@(str) ~isempty(strfind(str,'.:')),survey),1)
loc =
5
>>
  2 Comments
Mary
Mary on 8 May 2014
Thank you This works perfectly! I should have known I needed cellfun in there somewhere!!
dpb
dpb on 8 May 2014
Edited: dpb on 8 May 2014
Yeah, you need it for the operation on the result of the cell array from strfind or regexp to convert to an array on which you can apply isempty and not get just the collapsed result of a single value for the cell array as a whole. To that array you can then apply find to get the column. NB: that the logical array there might turn out to be useful as it is as the locations of the columns that can be used in a broader scope.
As a hint, I didn't actually write the anonymous function in one swell foop; it was from the inside out starting with the strfind, adding isempty then wrapping those...it's how most often one can get to the final form the quickest, I find, generally.

Sign in to comment.

More Answers (0)

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!