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!
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!