strfind when I have more than one row

4 visualizaciones (últimos 30 días)
joseph Frank
joseph Frank el 29 de Jun. de 2011
K = STRFIND(TEXT,PATTERN)
I am trying to find Pattern in an array of many rows but I am receiving an error : ??? Error using ==> strfind Input strings must have one row
Is there any function taht would substitute strfind in this case?
  1 comentario
Sean de Wolski
Sean de Wolski el 29 de Jun. de 2011
Does your pattern take up multiple rows?

Iniciar sesión para comentar.

Respuestas (4)

Matt Fig
Matt Fig el 29 de Jun. de 2011
You have a couple of choices with STRFIND. You could loop over the rows, or pad the array with NANs (or other irrelevant value) then reshape to one long string. For example:
A = [1 2 3 4 5 6;7 6 5 6 3 2;1 2 3 4 5 6]
for ii = 1:size(A,1)
L{ii} = strfind(A(ii,:),[5 6]); % Preall if large A.
end
B = [A nan(size(A,1),1)];
strfind(reshape(B.',1,[]),[5 6])

Paulo Silva
Paulo Silva el 29 de Jun. de 2011
a=['Congratulations'
'to Walter '
'for his 4000 '
'reputation '
'points ']
rc=[];
for r=1:size(a,1)
c=strfind(a(r,:),'Walter');
if ~isempty(c)
rc=[rc; r c]; %just in case the word appears multiple times
end
end
rc %first column is the row and second column is the column where the word is
  1 comentario
Roland
Roland el 29 de Dic. de 2011
This also does not work well. E.g. if one row has multiple matches this will cause an inconsistent dimensions error.

Iniciar sesión para comentar.


Teja Muppirala
Teja Muppirala el 30 de Jun. de 2011
STRFIND works on cells too:
a=['Congratulations'
'to Walter '
'for his 4000 '
'reputation '
'points '
'WalterWalterWal']
pat = 'Wal'
strfind(cellstr(a),pat)
  1 comentario
Roland
Roland el 29 de Dic. de 2011
This does not work well. E.g. if one row has multiple matches this will cause an inconsistent dimensions error.

Iniciar sesión para comentar.


Roland
Roland el 29 de Dic. de 2011
This is a possible solution ... not perfect though:
%
% Look for a in b. Returns cell array of
% strfind results for each row.
%
% Usage: strfindmultirow(<searchdata>, <searchpattern>);
%
% e.g. if:
%
% a=['Congratulations'
% 'to Walter '
% 'for his 4000 '
% 'reputation '
% 'points ']
%
% rc = strfindmultirow(a, 'a');
%
% yields:
% rc = strfindmultirow(a, 'a')
% ans = [1x2 double] [5] [] [6] []
%
function rc = strfindmultirow(a, b)
rc={};
for r=1:size(a,1)
c=strfind(a(r,:), b);
rc{length(rc) + 1} = c;
end

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by