/
achar.m
113 lines (108 loc) · 2.83 KB
/
achar.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
%% achar - find strings in an array of strings
% Gordon Bean, November 2010
% "achar" = "find" in Portuguese
%
% Usage
% indexes = achar( strings_to_find, array_of_strings );
% indexes = achar( strings_to_find, array_of_strings, keep_nans );
% indexes = achar( strings_to_find, array_of_strings, keep_nans, ...
% findall );
% indexes = achar( strings_to_find, array_of_strings, keep_nans, ...
% findall, verbose );
%
% This function finds the indexes of each query in the population
% * strings_to_find can be a single string or a cell array of strings.
% These are the strings you want to find.
% * array_of_strings is a cell array of strings, representing the
% population of strings in which you are seeking the strings_to_find.
% * keep_nans is a boolean indicating whether to retain NaN values when
% values in strings_to_find could not be found in array_of_strings.
% * findall is a boolean instructing ACHAR to find all occurrences of all
% elements of 'strings_to_find' in 'array_of_strings'. Note that when
% 'findall' is true, keep_nans is ignored.
% * verbose is a boolean indicating whether to print a warning
% statement when values could not be found.
%
% Examples
% >> foo = {'one','two','three','one'}
%
% foo =
%
% 'one' 'two' 'three' 'one'
%
% >> achar('two', foo)
%
% ans =
%
% 2
%
% >> achar({'one','three'}, foo)
%
% ans =
%
% 1 3
%
% >> achar({'three','four'}, foo)
%
% ans =
%
% 3
%
% >> achar({'three','four'}, foo, 1)
%
% ans =
%
% 3 NaN
%
% >> achar({'three','one'}, foo, 0, 1)
%
% ans =
%
% 3 1 4
%
function inds = achar (queries, population, keepnans, findall, verbose)
if (nargin < 5)
verbose = 0;
end
if (nargin < 4)
findall = 0;
end
if (nargin < 3)
keepnans = 0;
end
if (~iscell(queries))
queries = {queries};
end
if (~findall)
inds = nan(size(queries));
for a = 1 : numel(queries)
tmp = find(strcmpi(queries{a},population));
if (~isempty(tmp))
inds(a) = tmp(1);
else
inds(a) = nan;
end
end
else
inds = nan(size(population));
pos = 1;
for a = 1 : numel(queries)
tmp = find(strcmpi(queries{a},population));
if (~isempty(tmp))
inds(pos+(0:length(tmp)-1)) = tmp;
pos = pos + length(tmp);
else
% Do nothing. You cannot report missing values and multiple
% values at the same time.
end
end
end
if verbose
for a = find(isnan(inds))
fprintf(2,'ACHAR: Could not find %s (%i)\n',queries{a},a);
end
end
if (~keepnans || findall)
inds(isnan(inds)) = [];
end
end