Browse a database of images matlab
Mostra commenti meno recenti
i have folder contain 250 images i went to divided to 10 folders each one container 25 image
Risposte (1)
Abhishek
il 3 Apr 2025
To efficiently distribute images from a single directory into multiple subfolders, you can use the ‘copyfile’ method inside a loop. The script below copies images from a source directory into multiple destination folders, with each folder containing a specified number of images. I have tried it on MATLAB R2024b:
sourceDir = 'C:\Users\ SampleImages';
destDir = 'C:\Users \Output';
imageFiles = dir(fullfile(sourceDir, '*.png'));
imagesPerFolder = 25;
numFolders = ceil(length(imageFiles) / imagesPerFolder);
for folderIdx = 1:numFolders
folderName = fullfile(destDir, sprintf('Folder_%02d', folderIdx));
if ~exist(folderName, 'dir')
mkdir(folderName);
end
startIdx = (folderIdx - 1) * imagesPerFolder + 1;
endIdx = min(folderIdx * imagesPerFolder, length(imageFiles));
for imgIdx = startIdx:endIdx
sourceFile = fullfile(sourceDir, imageFiles(imgIdx).name);
destFile = fullfile(folderName, imageFiles(imgIdx).name);
copyfile(sourceFile, destFile);
end
end
disp('Images have been copied into folders successfully.');
Here is the step-by-step process:
- The code first calculates the number of folders to be created and then creates those folders.
- It then distributes the images among these folders. The variable ‘imagesPerFolder’ defines the number of images to be placed in each destination folder.
- The ‘copyfile’ method is used to copy the files from the source to the destination folder. Alternatively, you can use ‘movefile’ to move the files instead of copying them.
Please refer to the documentations for more details:
- copyfile: https://www.mathworks.com/help/releases/R2024b/matlab/ref/copyfile.html
- movefile: https://www.mathworks.com/help/releases/R2024b/matlab/ref/movefile.html
I hope this solves your problem. Thanks!
Categorie
Scopri di più su Images in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!