Find Which Column contains a pattern

2 visualizaciones (últimos 30 días)
Mary
Mary el 8 de Mayo de 2014
Editada: dpb el 8 de Mayo de 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'

Respuesta aceptada

dpb
dpb el 8 de Mayo de 2014
>> loc=find(cellfun(@(str) ~isempty(strfind(str,'.:')),survey),1)
loc =
5
>>
  2 comentarios
Mary
Mary el 8 de Mayo de 2014
Thank you This works perfectly! I should have known I needed cellfun in there somewhere!!
dpb
dpb el 8 de Mayo de 2014
Editada: dpb el 8 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by