-
Notifications
You must be signed in to change notification settings - Fork 49
/
getAllFiles.m
59 lines (55 loc) · 2 KB
/
getAllFiles.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
56
57
58
59
function fileList = getAllFiles(dirPath)
% Retrieves a list of all files within a directory
%
% Syntax: fileList = getAllFiles(dirName)
%
% Inputs:
% dirPath - The relative or full path of the directory to recursivley
% search.
%
% Outputs:
% fileList - A cell array list of the full path for each file found.
%
% Example:
% searchPath = [matlabroot filesep 'examples'];
% files = getAllFiles(searchPath);
%
%
% Author: Jacob Donley
% University of Wollongong
% Email: [email protected]
% Date: 22 January 2015
% Revision: 0.1
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Just incase this function tries to recursively call within a class folder we
% should create a function handle for this function to use
infun = dbstack('-completenames');
funcName = infun.name;
funcPath = infun.file;
classDirs = getClassDirs(funcPath);
thisFuncHandle = str2func([classDirs funcName]);
% Find the files
dirData = dir(dirPath); % Get the data for the current directory
dirIndex = [dirData.isdir]; % Find the index for directories
fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files
if ~isempty(fileList)
fileList = cellfun(@(x) fullfile(dirPath,x),... % Prepend path to files
fileList,'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name}; % Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'}); % Find index of subdirectories that are not '.' or '..'
for iDir = find(validIndex) % Loop over valid subdirectories
nextDir = fullfile(dirPath,subDirs{iDir}); % Get the subdirectory path
fileList = [fileList; thisFuncHandle(nextDir)]; % Recursively call getAllFiles
end
end
function classDirs = getClassDirs(FullPath)
classDirs = '';
classes = strfind(FullPath,'+');
for c = 1:length(classes)
clas = FullPath(classes(c):end);
stp = strfind(clas,filesep);
classDirs = [classDirs clas(2:stp(1)-1) '.'];
end
end