-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFileIterator.m
55 lines (48 loc) · 1.5 KB
/
FileIterator.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
classdef FileIterator < handle
%FileIterator Conveniently loop through files.
% Pass in a relative directory and a glob, and return an iterator
% that returns the relative path of all the files.
%
% Example:
%
% folder = 'my/images/are/here';
% glob = '*.tif';
% imgs = FileIterator(folder, glob);
% while(imgs.more())
% img = imread(imgs.next());
% end
properties (Hidden, SetAccess = protected)
current = 1;
filenames = {};
end
properties (SetAccess = protected)
length = 0;
end
methods
function iterator = FileIterator(folder, glob)
fullFolder = fullfile(folder, '/');
files = dir([fullFolder, glob]);
iterator.length = length(files);
iterator.filenames = cell(iterator.length, 1);
for i = 1:iterator.length
filename = files(i).name;
iterator.filenames{i} = fullfile(folder, '/', filename);
end
end
function filename = next(self)
if ~self.more()
filename = '';
else
filename = self.filenames{self.current};
self.current = self.current + 1;
end
end
function more = more(self)
more = self.current <= self.length;
end
function iterator = reset(self)
self.current = 1;
iterator = self;
end
end
end