Searching for group of letters in a structure or character string
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Oleg Komarov
2012-3-12
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)
更多回答(1 个)
Jacob Halbrooks
2012-3-12
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))
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!