File Exchange

image thumbnail


version 1.1.2 (2.71 KB) by wen-feng Huang
Search strings that match multiple patterns with a single line of code


Updated 18 Oct 2019

View License

CONTAINSTR returns TRUE if PATTERN is found in any element of string array S.

Sometimes, I need to search strings that match multiple patterns.
The use of strfind (regexp) combing with for-loop (cellfun) makes the script look long and messy.
In newer version (R2016b), it is easy to do in a single line of code.
Just call the built-in function 'contains', that easily determine if pattern is in string.

So, I created this function for users with older version to search pattern in text.
This function follows the same behaviour as 'contains'.
No more for-loop is needed.

Cite As

wen-feng Huang (2020). containstr (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)


A small improvement:
This considers cell string matrices only:
[rr, cc] = size(s);
ns = rr * cc;
tf = false(rr, cc);
Use this to be independent from the number of dimensions:
tf = false(size(s));
for ii = 1:numel(s)



fix bug


rewrite a concise version, and add 'regexp' engine


clear description

MATLAB Release Compatibility
Created with R2017a
Compatible with any release
Platform Compatibility
Windows macOS Linux