Borrar filtros
Borrar filtros

Find specific character sequence in a list of strings

7 visualizaciones (últimos 30 días)
Hi, I have a table (.csv file) containing various letter strings on the first column and other properties on the other columns.
1) I need to find all the entries that have matching any 4-letter sequences anywhere in the first column strings, and make another table containing just the matched entries. E.g., rows 6 & 7 have "GNNR" matching.
2) I want to find all the entries in the table that contain, specifically, either : "GLWS" or " GIWS" (in that order of characters) and make another table with those.
Thanks!

Respuesta aceptada

Image Analyst
Image Analyst el 13 de Feb. de 2023
So I assume you did the super obvious, brute force method of using a simple for loop to go down the list using strfind or contains to see if the item has the desired string in it. But what happened? Why didn't that work? Show your code.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
  2 comentarios
Mihaela Mihailescu
Mihaela Mihailescu el 13 de Feb. de 2023
No, I did not do that. I was hoping to get a hint from you, since I'm kind of new to this. (-:
Can you give me an example. Thanks!
Image Analyst
Image Analyst el 13 de Feb. de 2023
You didn't read the link I gave you, did you? I know because you keep forgetting to attach your data.
Assuming your strings are in a table called t, and a cell array field called ID
ID = t.ID;
numRows = numel(ID);
% Make logical vectors that say whether the string is in the row or not.
GNNR = false(numRows, 1);
GLWS = false(numRows, 1);
for k = 1 : numRows
thisCell = ID{k};
if contains(thisCell, 'GNNR')
GNNR(k) = true;
end
if contains(thisCell, 'GLWS')
GLWS(k) = true;
end
end
% Get table with GNNR rows in it
t2 = t(GNNR, :)
% Get table with GLWS rows in it
t3 = t(GLWS, :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays 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