-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflist_select.m
More file actions
100 lines (86 loc) · 2.27 KB
/
flist_select.m
File metadata and controls
100 lines (86 loc) · 2.27 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
function [list, sel] = flist_select(list,varargin)
% list = flist_select(list,field,value,field2,value2, ...)
%
% select elements of a structure based on value of fields.
%
% input:
% list: a vector structure array. Typically describing a list of
% items (directory listing) with various attributes stored in
% several fields
% following arguments come in pairs:
% field: a string for the name of a field
% value: a target value for that field
%
% list = flist_select(list,field,value, 'fun', fun, ...)
%
% additionally apply the function fun (a function handle) to field-value
% pairs
%
% example: flist_select(list,'size',19000,'fun',@gt)
% will select elements of list that have a size field greater than
% 19000
%
%
% list = flist_select(list,field,value, 'inv', ...)
%
% invert selection.
%
%
% Maximilien Chaumon 2017
% parse arguments
todel = [];
inv = 0;
fun = {};
for iv= 1:2:numel(varargin)
if strcmp(varargin{iv},'fun')
fun{end+1} = varargin{iv+1};
todel(end+1:end+2) = [iv iv+1];
elseif strcmp(varargin{iv},'inv')
inv = 1;
todel(end+1) = [iv];
end
end
varargin(todel) = [];
if isempty(fun)
fun(1:numel(varargin)/2) = repmat({[]},numel(varargin)/2,1);
end
% scan fields
sel = true(size(list));
if isempty(list)
return
end
for iv = 1:2:numel(varargin)
if isempty(varargin{iv+1})
continue
end
% depending on the type of data in each field
if isempty(fun{(iv+1)/2})
if isnumeric(list(1).(varargin{iv}))
fun{(iv+1)/2} = @eq;
elseif ischar(list(1).(varargin{iv}))
if strncmp(varargin{iv+1},'~',1)
varargin{iv+1}(1) = [];
fun{(iv+1)/2} = @(x,y)isempty(regexp(x,y, 'once'));
else
fun{(iv+1)/2} = @(x,y)~isempty(regexp(x,y, 'once'));
end
end
end
for i = 1:numel(list)
if ~isempty([list(i).(varargin{iv})])
sel(i) = sel(i) & fun{(iv+1)/2}([list(i).(varargin{iv})],varargin{iv+1});
else
sel(i) = false;
end
end
end
if inv
if islogical(sel)
sel = ~ sel ;
else
nusel = 1:numel(list);
nusel(sel) = [];
sel = nusel;
end
end
list = list(sel);