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

采纳的回答

Oleg Komarov
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
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 CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by