MATLAB Answers

How to select images from nested folder?

2 views (last 30 days)
Farman Shah
Farman Shah on 16 Jul 2019
Commented: Farman Shah on 20 Jul 2019
I have a folder named AAA. Inside folder AAA there is another folder name A, inside A there is another folder B. folder B consist of 300 images. I want to get every 10th number image from folder B ( It will be 30 images) and place these images in another folder C. I need a Matlab code for this Scenario. Any help?

Accepted Answer

Joel Handy
Joel Handy on 16 Jul 2019
Edited: Joel Handy on 16 Jul 2019
Something like this should work. You will possibly need to add code to appropriately sort your list of images. I've marked where you would do it.
imageFolder = uigetdir('', 'Select Image Folder');
destinationFolder = uigetdir(imageFolder, 'Select Destination Folder');
contents = dir(imageFolder);
fileList = contents(~[contents.isdir]);
% Sort list as needed here
filesToMove = fileList(1:10:end);
fromPaths = fullfile(imageFolder,{filesToMove.name});
toPaths = fullfile(destinationFolder,{filesToMove.name});
for fileIdx = 1:numel(fromPaths)
copyfile(fromPaths{fileIdx}, toPaths{fileIdx})
end
Also I'm note sure the specific relevance of folders AAA and A. If AAA, A, and B are all known folders and you want to create C based on them, you could replace the uigetdir code with something like
imageFolder = fullfile(AAA,A,B);
destinationFolder = fullfile(AAA,A,C);
if ~exist(destinationFolder, 'dir')
mkdir(festinationFolder);
end
  4 Comments
Farman Shah
Farman Shah on 20 Jul 2019
Yeah I was doing a project and it is done now. Thank you
your code was good. For those others comming on this thread, it is helpfull.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 18 Jul 2019
Have you looked at imageDatastore()? Or dir() using two asterisks as the file pattern?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!