Find Which Column contains a pattern

4 次查看(过去 30 天)
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