
%% Find Supported Files
%
% Search input directory for files of requested type.
%

%% Input Arguments

% dirPath - Input directory
%           type: String

% suppFiles - Support file types
%             type: Nx1 cell

% filter - Optional regular expression to filter files 
%          type: String


%% Output Arguments

% matNames -  Names of files with matching requested type(s)
%             type: nFilesx1 cell

% nMats - Number of files matching requested type(s)
%         type: integer (nonnegative) 


%% Find Supported Files

function [matNames, nMats] = find_supported_files(dirPath, suppFiles, varargin)

assert(ischar(dirPath)); 
assert(isa(suppFiles, 'cell'));
if nargin > 2
    filter = varargin{1};
    assert(ischar(filter)); 
else
    filter = '.';
end

dirFiles  = dir(dirPath);
fileNames = {dirFiles.name};
nFiles = length(dirFiles);

% Find all supported files within directory
matNames = cell(nFiles, 1);
nMats = 0;
for i = 1:nFiles,
   
    [~,~,ext] = fileparts(fileNames{i});
    ext = regexprep(ext, '[.]', '');
    
    if any(strcmp(ext, suppFiles)) && ~isempty(regexp(fileNames{i}, filter, 'once'))
        matNames{nMats+1} = fileNames{i};
        nMats = nMats + 1;
    end
    
end

matNames = matNames(1:nMats);

end