Find specific character sequence in a list of strings
7 views (last 30 days)
Show older comments
Mihaela Mihailescu
on 13 Feb 2023
Commented: Image Analyst
on 13 Feb 2023
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 Comments
Accepted Answer
Image Analyst
on 13 Feb 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 Comments
Image Analyst
on 13 Feb 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, :)
More Answers (0)
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!