-
Notifications
You must be signed in to change notification settings - Fork 8
/
DirectoryIterator.m
44 lines (37 loc) · 1.13 KB
/
DirectoryIterator.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
classdef DirectoryIterator < handle
%DirectoryIterator Conveniently loop through directories.
properties (Hidden, SetAccess = protected)
current = 1;
directories = {};
end
properties (SetAccess = protected)
length = 0;
end
methods
function iterator = FileIterator(folder)
fullFolder = fullfile(folder, '/');
files = dir(fullFolder);
iterator.length = length(files);
iterator.dirnames = cell(iterator.length, 1);
for i = 1:iterator.length
folder = files(i).name;
iterator.dirnames{i} = fullfile(folder, '/', folder);
end
end
function dirname = next(self)
if ~self.more()
dirname = '';
else
dirname = self.dirnames{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