Matlab cellfun by strfind function

I want to use a function cellfunfor a function strfindto find the index of each row in an array of row cells in another array of row cells to exclude them from it.

strings = {'aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii','jjj'};
excludedStrings = {'b','g','h'};
idx = cellfun('strfind',strings,excludedStrings);
idx = cell2mat = idx;
idx = reshap(idx,numel(idx),1);
idx = unique(idx);
strings(cell2mat(idx)) = [];

Error in the call bar cellfun, how can I fix it?

+3
source share
2 answers

Here's a nice one line:

strings = regexprep(strings, excludedStrings, '');

Structure:

  • All words / characters to search are transmitted to regexprep
  • This function replaces every occurrence of any word / symbol in the set specified above with an empty string ( '').

It will automatically repeat this action for all elements of the cell array string.

string, :

strings = strings(~cellfun('isempty', strings));
+3

, :

idx = cellfun(@(str) any(cellfun(@(pat) any(strfind(str,pat)),excludedStrings)),strings)

idx =
    0     1     0     0     0     0     1     1     0     0

, , :

strings(idx) = [];

, ( ), cellfun s.

+2

Source: https://habr.com/ru/post/1617011/


All Articles