-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjpa_getLogicalID.m
More file actions
98 lines (90 loc) · 2.9 KB
/
jpa_getLogicalID.m
File metadata and controls
98 lines (90 loc) · 2.9 KB
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
function [logID, indSearchIn, idsFound] = jpa_getLogicalID(searchIn, searchFor, varargin)
% Function that searches searchFor-Array in String-Array(searchIn).
% the function gives the a logical vector back which searchFor was found
% an the position in searchIn where searchFor was first found. IF SearchFor
% was not found Index will be set to 0
%
% Syntax:
% jpa_getLogicalID(searchIn, searchFor[, excludeList, includeList])
%
% Inputs:
% searchIn - String-Array to search in
% searchFor - String-Array to searcFor
% excludeList - String-Array containing exclude-SearchFor
% includeList - String-Array containing include-SearchFor
%
% Outputs:
% logID - logical Array which SearchFor was found
% indSearchIn - Index where SearchFor was found in SearchIn
% idsNotFound - logical Array which shows the IDs which could not
% be found
%
% Example:
% jpa_getLogicalID({'A' 'AB' 'C'}, {'A' 'C'})
% -> output: [1 1 1] [1 3] [0 0]
%
% jpa_getLogicalID({'A' 'AB' 'C'}, {'A' 'C'}, {'A'})
% -> output: [0 0 1] [0 3] [0 0]
%
% jpa_getLogicalID({'A' 'AB' 'C'}, {'A' 'D'}, {''}, {'A'})
% -> output: [1 1 0] [1 0] [0 1]
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% See also:
% Author: Jan Albrecht
% Work address:
% email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
% Website:
% Sep 2015; Last revision: 13-Okt-2015
%------------- BEGIN CODE --------------
%% test input arguments
if nargin < 2
error('Wrong number of Input-Arguments!');
elseif nargin ==2
excludeList = {''};
includeList = searchFor;
elseif nargin ==3
excludeList = varargin{1};
includeList = searchFor;
else
excludeList = varargin{1};
includeList = varargin{2};
end
%% Initialize
logID = false(length(searchFor),1);
idsFound = false(length(searchFor),1);
indSearchIn = zeros(length(searchIn),1);
%% Loop through searchFor
for indID=1:1:length(searchFor)
%% search ID in searchIn
res = strfind(searchIn,searchFor{indID});
emptyCells = ~cellfun(@isempty,res);
if sum(emptyCells) == 0
continue;
end
% set the logical bit to true because we found the ID
idsFound(indID) = true;
%% search ID in exludeList
if ~isempty(cell2mat(strfind(excludeList,searchFor{indID})))
continue;
end
%% search ID in includeList
if isempty(cell2mat(strfind(includeList,searchFor{indID})))
continue;
end
%% case we found ID in searchIn
% get index where we found ID in searchIn
ind = find(not(cellfun('isempty',res)));
if length(ind) > 1
disp('ID was found more than once!... Take first occurrence!')
end
% write at searchIn-found-position the Index of the ID
indSearchIn(ind(1,1)) = indID;
% set logical to true
logID(indID) = true;
end
end
%------------- END CODE --------------