Azzera filtri
Azzera filtri

Searching for group of letters in a structure or character string

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

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

Più risposte (1)

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

Community Treasure Hunt

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

Start Hunting!

Translated by