In the example function you gave the second input 'fileType' is not used, is that the problem?
You could use this in the line calling fullfile to get only the filetypes you want, i.e.:
function [filenames] = GenerateImageList(dirname, fileType)
fileIdentity = fullfile(dirname, ['*' fileType]);
files = dir(fileIdentity);
for i = 1:length(files)
filenames = files(i).name;
fprintf('%s\n', filenames);
end
end
For example you can run this code as:
filenames = GenerateImageList(pwd,'png')
filenames = GenerateImageList(pwd,'jpg')
However, there are some problems with your code - if there are no files with the extension you ask for the function throws an error because filenames is left undefined. Also, I think you want filenames to contain a list of all the filenames? But the way it is written here it will only ever contain the last filename it found because you overwite it on every loop. Might I suggest something like this:
function [filenames] = GenerateImageList(dirname, fileType)
fileIdentity = fullfile(dirname, ['*' fileType]);
files = dir(fileIdentity);
filenames = {files(:).name};
fprintf(1, '%s\n', filenames{:})
end
If you don't need to display the text in the command window for every filename (why do you want that?) you can just call dir directly for the filename list:
function [filenames] = GenerateImageList(dirname, fileType)
df = dir(fullfile(dirname, ['*' fileType]));
filenames = {df(:).name};
end
And then maybe you don't need the GenerateImageList function at all, you could just put those lines of code directly inb the parent function...
Hope this helps,
M.
Best Answer