How can I filter struct according to strings in field?

16 views (last 30 days)
I have 1132x1 struct array with fields:
Geometry
BoundingBox
X
Y
STYP
In field STYP can be strings like EC030, EB010, EA040, etc... For me are only important rows (in struct) with string EA030 (in field STYP).

Accepted Answer

Guillaume
Guillaume on 12 Apr 2017
wantedelements = yourstructure(strcmp({yourstructure.STYP}, 'EA030'))
{yourstructure.STYP} concatenate all the STYP fields into a cell array that you can then compare to your string to filter your array.
  2 Comments
Candice Hermant
Candice Hermant on 9 Jun 2021
Edited: Candice Hermant on 9 Jun 2021
Hi!
I know this was answered a few years ago but is there a way to use this syntax with regex? What I mean is instead of giving a string like 'EA030' here, I would like to retrieve all the elements of a field that match a specific pattern, for instance all the text files.
I tried this:
wantedelement = foldersinfo(strcmp({foldersinfo.name}, '*.txt'))
but it returns an empty struct array.
Thank you very much for your help in advance.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures 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!