-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrm_frompath.m
More file actions
105 lines (96 loc) · 2.57 KB
/
rm_frompath.m
File metadata and controls
105 lines (96 loc) · 2.57 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
99
100
101
102
103
104
105
function rm_frompath(what)
p = [path pathsep];
ps = regexp(p,['(.*?)' pathsep],'tokens');
for i = 1:numel(ps)
ps{i} = ps{i}{1};
end
ps = ps(regexpcell(ps,what,'inv'));
p = [];
for i= 1:numel(ps)
p = [p pathsep ps{i}];
end
path(p);
function idx = regexpcell(c,pat, cmds)
% idx = regexpcell(c,pat, cmds)
%
% Return indices idx of cells in c that match pattern(s) pat (regular expression).
% Pattern pat can be char or cellstr. In the later case regexpcell returns
% indexes of cells that match any pattern in pat.
%
% cmds is a string that can contain one or several of these commands:
% 'inv' return indexes that do not match the pattern.
% 'ignorecase' will use regexpi instead of regexp
% 'exact' performs an exact match (regular expression should match the whole strings in c).
% 'all' (default) returns all indices, including repeats (if several pat match a single cell in c).
% 'unique' will return unique sorted indices.
% 'intersect' will return only indices in c that match ALL the patterns in pat.
%
% v1 Maximilien Chaumon 01/05/09
% v1.1 Maximilien Chaumon 24/05/09 - added ignorecase
% v2 Maximilien Chaumon 02/03/2010 changed input method.
% inv,ignorecase,exact,combine are replaced by cmds
narginchk(2,3)
if not(iscellstr(c))
error('input c must be a cell array of strings');
end
if nargin == 2
cmds = '';
end
if not(isempty(regexpi(cmds,'inv', 'once' )))
inv = true;
else
inv = false;
end
if not(isempty(regexpi(cmds,'ignorecase', 'once' )))
ignorecase = true;
else
ignorecase = false;
end
if not(isempty(regexpi(cmds,'exact', 'once' )))
exact = true;
else
exact = false;
end
if not(isempty(regexpi(cmds,'unique', 'once' )))
combine = 2;
elseif not(isempty(regexpi(cmds,'intersect', 'once' )))
combine = 3;
else
combine = 1;
end
if ischar(pat)
pat = cellstr(pat);
end
if exact
for i_pat = 1:numel(pat)
pat{i_pat} = ['^' pat{i_pat} '$'];
end
end
for i_pat = 1:length(pat)
if ignorecase
trouv = regexpi(c,pat{i_pat}); % apply regexp on each pattern
else
trouv = regexp(c,pat{i_pat}); % apply regexp on each pattern
end
idx{i_pat} = find(not(cellfun('isempty',trouv)));
end
if isempty(pat)
idx = {};
end
makevector = @(x)(x(:));
switch combine
case 1
idx = makevector([idx{:}]);
case 2
idx = unique([idx{:}]);
case 3
for i_pat = 2:length(pat)
idx{1} = intersect(idx{1},idx{i_pat});
end
idx = idx{1};
end
if inv % if we want to invert result, then do so.
others = 1:numel(trouv);
others(idx) = [];
idx = others;
end