MATLAB: How to use waitbar while copying images from folders and subfolders

-

I have stored the images in a folder named 'dataset' , which contains subfolders as 'red' , 'brown' , 'black' , and each folder contains abt 50 images. I want to copy these images into a folder named 'testing data' , meanwhile , while copying the images I want to display a waitbar which shows the copying process , I did try out somethings but couldn't succeed .The code is as :
clear all
clc
M_dir = 'C:\Users\Dell\Documents\MATLAB\dataset\';% source directory
D_dir = 'C:\Users\Dell\Documents\MATLAB\testing_data\';
files = dir(M_dir);
dirFlags = [files.isdir];
subFolders = files(dirFlags);
mkdir testing_data
for k = 1 :length(subFolders)
if any(isletter(subFolders(k).name))
c_dtry = strcat(M_dir,subFolders(k).name);
fileList = getAllFiles(c_dtry);
h = waitbar(0,'Please wait...');
for i=1:150
for n1 = 1:length(fileList)
[pathstr,name,ext] = fileparts(fileList{n1})% file type
Im = imread(fileList{n1});
f=str2num(name);
if(f>=1 & f<=50)
baseFileName = strcat(name,ext);
imwrite(Im, fullfile(D_dir, baseFileName));
elseif(f>=59 & f<=83)
baseFileName = strcat(name,ext);
imwrite(Im, fullfile(D_dir, baseFileName));
elseif(f>=109 & f<=133)
baseFileName = strcat(name,ext);
imwrite(Im, fullfile(D_dir, baseFileName));
end
end
waitbar(i/80,h)
end
end
end
Basically ,I have tried in many places but could not find the answer, I would appreciate it if you could help me to solve this problem !!! Thank you 🙂

Best Answer

What I would do is first make a list of all sources and targets, and then do the actual copying in a loop like this
h_wait=waitbar(0,'copying...');
N=numel(sources);
for n=1:N
copyfile(sources{n},destinations{n})
waitbar(n/N,h_wait)
end
That should work