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 Version History

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 (2021). containstr (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Chang hsiung


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)

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

Community Treasure Hunt

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

Start Hunting!