Fetching image files from all sub folders within a folder
Mostrar comentarios más antiguos
I am using following code to fetch the files in a folder. However my folder does contain some subfolders that also contain images. How can I fetch all the possible .jpg files in subfolders too.
directory = uigetdir();
files = dir(fullfile(directory, '*.jpg'));
I have also tried, it doesnt list any file
function files = getFilesFromDirectory(mainSelectedFolder)
files = [];
folderNamesList = {};
filenames = [];
mainIndex = 0;
% Fetchign all possible sub folders
subFoldersList = genpath(mainSelectedFolder);
while true
[subfolder, subFoldersList] = strtok(subFoldersList, ';');
if isempty(subfolder)
break;
end
folderNamesList = [folderNamesList subfolder];
end
for x = 1 : length(folderNamesList)
selectedFolder = folderNamesList{x};
pngFiles = sprintf('%s/*.png', selectedFolder);
filenames = dir(pngFiles);
filePattern = sprintf('%s/*.jpg', selectedFolder);
filenames = [filenames; dir(filePattern)];
imagesCount = length(filenames);
if imagesCount >= 1
% fetching the full path names
for y = 1 : imagesCount
fullFileName = fullfile(selectedFolder, filenames(y).name);
files(mainIndex) = fullFileName;
end
end
end
2 comentarios
Stephen23
el 22 de Sept. de 2018
@Muhammad Umar: what version of MATLAB are you using?
Muhammad Umar
el 22 de Sept. de 2018
Respuestas (2)
Walter Roberson
el 22 de Sept. de 2018
files = dir(fullfile(directory, '**', '*.jpg'));
This requires 2016ish (I would need to investigate the exact release)
Image Analyst
el 22 de Sept. de 2018
0 votos
Or you could try imageDataStore().
Categorías
Más información sobre Blocked Images en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!