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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by