File Exchange

image thumbnail

containstr

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

0 Downloads

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 (https://www.mathworks.com/matlabcentral/fileexchange/70334-containstr), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Jan

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)

Updates

1.1.2

fix bug

1.1.1

rewrite a concise version, and add 'regexp' engine

1.0.1

clear description

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