Find specific character sequence in a list of strings

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

采纳的回答

Image Analyst
Image Analyst 2023-2-13
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 个评论
Mihaela Mihailescu
Mihaela Mihailescu 2023-2-13
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 2023-2-13
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, :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by