-
Notifications
You must be signed in to change notification settings - Fork 5
/
cat_io_contains.m
95 lines (83 loc) · 2.79 KB
/
cat_io_contains.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function TF = cat_io_contains(str,pat,~,icTF)
%Support contains function for older matlabs.
%
% TF = cat_io_contains(str,pat[,'ignoreCase',TRUE|FALSE])
%
% See also contains or use cat_io_contains(1) to run the unit test.
% ______________________________________________________________________
%
% Christian Gaser, Robert Dahnke
% Structural Brain Mapping Group (https://neuro-jena.github.io)
% Departments of Neurology and Psychiatry
% Jena University Hospital
% ______________________________________________________________________
% $Id$
switch nargin
case 0
help cat_io_contains;
case 1
unittest;
case {2,4}
if ischar(str), str = cellstr(str); end
if ischar(pat), pat = cellstr(pat); end
if ~iscellstr(str)
error('error:cat_io_contains','First argument must be text.')
end
if ~iscellstr(pat)
error('error:cat_io_contains','Search term must be a text or pattern array.')
end
if nargin == 4 && icTF
str = lower(str);
pat = lower(pat);
end
TF = ~cellfun('isempty', strfind( str , pat(1))); %#ok<STRCL1>
if numel(pat) > 1
for str2i = 2:numel(pat)
TF = TF | ~cellfun('isempty', strfind( str , pat{str2i}));
end
end
otherwise
error('error:cat_io_contains:badInput','Wrong number of input elements!');
end
end
% ======================================================================
function unittest
%unittest with cases from the MATLAB contains help.
strpats = {
{ {'Mary Ann Jones','Paul Jay Burns','John Paul Smith'} {'Paul'} };
{ {'Mary Ann Jones','Christopher Matthew Burns','John Paul Smith'} {'Ann','Paul'} };
{ {'Mary Ann Jones','Christopher Matthew Burns','John Paul Smith'} 'Ann' };
{ {'Mary Ann Jones','Christopher Matthew Burns','John Paul Smith'} 'ann' };
{ 'peppers, onions, and mushrooms' 'onion'};
{ 'peppers, onions, and mushrooms' 'nothing'};
{ 1:2 'test'};
{ 'test' 1};
};
for spi = 1:numel(strpats)
cat_io_cprintf('blue','\nTestcase %d:\n',spi)
fprintf('Input1: '); disp(strpats{spi}{1})
fprintf('Input2: '); disp(strpats{spi}{2})
for cs = 0:1
fprintf('contains: ');
try
if cs
disp(contains(strpats{spi}{1},strpats{spi}{2},'IgnoreCase',1))
else
disp(contains(strpats{spi}{1},strpats{spi}{2}))
end
catch e
cat_io_cprintf('err',[e.message '\n']);
end
fprintf('cat_io_contains: ');
try
if cs
disp(cat_io_contains(strpats{spi}{1},strpats{spi}{2},'IgnoreCase',1))
else
disp(cat_io_contains(strpats{spi}{1},strpats{spi}{2}))
end
catch e
cat_io_cprintf('err',[e.message '\n']);
end
end
end
end