If you are using R2016b or R2017a you can just use contains:
>> strings = {'ab', 'bc', 'de', 'fa'};
>> substrs = {'a', 'b', 'c'};
>> contains(strings, substrs)
ans =
1×4 logical array
1 1 0 1
Contains is also the fastest, especially if you use the new string datatype.
function profFunc()
strings = {'ab', 'bc', 'de', 'fa'};
substrs = {'a', 'b', 'c'};
n = 10000;
tic;
for i = 1:n
substrs_translated = regexptranslate('escape', substrs);
matches = false(size(strings));
for k = 1:numel(strings)
matches(k) = any(~cellfun('isempty', regexp(strings{k}, substrs_translated)));
end
end
toc
tic;
for i = 1:n
cellfun(@(s)any(~cellfun('isempty', regexp(s, substrs))), strings);
end
toc
tic;
for i = 1:n
pattern = ['(', strjoin(regexptranslate('escape', substrs), '|'), ')'];
output = ~cellfun('isempty', regexp(strings, pattern)); %#ok<NASGU>
end
toc
tic;
for i = 1:n
contains(strings,substrs);
end
toc
%Imagine you were using strings for all your text!
strings = string(strings);
tic;
for i = 1:n
contains(strings,substrs);
end
toc
end
Timing results:
>> profFunc
Elapsed time is 0.643176 seconds.
Elapsed time is 1.007309 seconds.
Elapsed time is 0.683643 seconds.
Elapsed time is 0.050663 seconds.
Elapsed time is 0.008177 seconds.