select folder that also includes subfolders

4 views (last 30 days)
I'd like to ask is it possible for uigetfile to select a folder so that the contents subfolders inside would get selected too
Here's my CNN code so far:
myfolder = 'D:\School Files\2021 - 2022 1st semester\ECE21131\fall dataset';
dataDir = fullfile(myfolder);
imdir = fullfile(dataDir);
imds = imageDatastore(imdir, "IncludeSubfolders",true ,"LabelSource","foldernames");
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.8,'randomized');
imageSize = [240 320 3];
layers = [
imageInputLayer(imageSize)
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,"Stride",2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride',2)
convolution2dLayer(3,64,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride',2)
convolution2dLayer(3,128,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(6)
softmaxLayer
classificationLayer];
%trainetwork
valfrequency = floor(numel(imdsTrain.Files)/64);
options = trainingOptions('adam', ...
'MaxEpochs',32, ...
'InitialLearnRate',3e-4, ...
'Shuffle','every-epoch', ...
'MiniBatchSize',64,...
'ValidationData',imdsValidation, ...
'ValidationFrequency',valfrequency, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
Yvalidation = imdsValidation.Labels;
accuracy = mean(YPred == Yvalidation);
idx = [1 4 8 12 16 20 24 28 32 36 40 41 42];
figure
for i = 1:numel(idx)
subplot(5,5,i)
I = readimage(imdsValidation,idx(i));
label = YPred(idx(i));
imshow(I)
title(char(label))
end
whereas the first line would be replaced by a uigetfile
the "fall dataset" folder has 6 subfolders which would also be fed into the system for the CNN
so basically, I'd like a way something like uigetfile that takes in all .png files within a folder, and its subfolders

Answers (1)

dpb
dpb on 10 Dec 2021
There is no builtin gui tool that does more than multi-select on files within a single folder; you have to traverse the directory tree programmatically. There are several submissions on FEX that do it and while I don't have a link to it, I know I've seen a very nice demonstration posted here as well by one of the several other MVPs -- I don't recall which just now.
I've complained for years that the MATLAB builtin dir() function doesn't accept switches such as /s -- it would be so handy for such cases if it did -- instead, most of the time I just build the appropriate dir command string including the wildcard filename expression and pass it to the OS and then iterate through the resulting returned array.

Community Treasure Hunt

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

Start Hunting!