Clear Filters
Clear Filters

Find specific character sequence in a list of strings

7 views (last 30 days)
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!

Accepted Answer

Image Analyst
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
Mihaela Mihailescu
Mihaela Mihailescu on 13 Feb 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 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, :)

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!