Searching for group of letters in a structure or character string
1 view (last 30 days)
Show older comments
Hi all,
I am working with a structure that contains various pieces of mixed data and having trouble sifting through it to find necessary pieces of info. For example, Structure(1).field contains eth:ip:udp:dns and Structure(169).field contains eth:ip:tcp.
I would like to efficiently find, for example, all elements that contain tcp.
If anyone could help out I would really appreciate it.
Thanks, Adam
0 Comments
Accepted Answer
Oleg Komarov
on 12 Mar 2012
Suppose your structure is:
S(1).field = 'eth:ip:udp:dns';
S(2).field = 'eth:ip:tcp';
S(3).field = 'eth:ip';
idx = ~cellfun('isempty',strfind({S.field},'tcp'));
S(idx)
More Answers (1)
Jacob Halbrooks
on 12 Mar 2012
I would suggest you write a single-input function that takes a struct and returns whether that struct contains "tcp". Once you have such a function, use ARRAYFUN on your struct array to return a logical mask of the structs that contain the data you want, and use logical indexing to filter your array. For example:
dummyData = [struct('f','eth:ip:udp:dns') struct('f','eth:ip:tcp')];
testFcn = @(x)~isempty(strfind(x.f,'tcp'));
filteredData = dummyData(arrayfun(testFcn, dummyData))
See Also
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!