Searching for group of letters in a structure or character string

4 vues (au cours des 30 derniers jours)
Adam Clark
Adam Clark le 12 Mar 2012
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

Réponse acceptée

Oleg Komarov
Oleg Komarov le 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)

Plus de réponses (1)

Jacob Halbrooks
Jacob Halbrooks le 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))

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by