Find Which Column contains a pattern

조회 수: 2 (최근 30일)
Mary
Mary 2014년 5월 8일
편집: dpb 2014년 5월 8일
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'

채택된 답변

dpb
dpb 2014년 5월 8일
>> loc=find(cellfun(@(str) ~isempty(strfind(str,'.:')),survey),1)
loc =
5
>>
  댓글 수: 2
Mary
Mary 2014년 5월 8일
Thank you This works perfectly! I should have known I needed cellfun in there somewhere!!
dpb
dpb 2014년 5월 8일
편집: dpb 2014년 5월 8일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by