Clear Filters
Clear Filters

Select folders one by one using imageDatastore function

5 views (last 30 days)
Dear all,
I have a "Parent_Folder" and inside it I have another 5-subfolders that contain images inside them: Folder_1, Folder_2, Folder_3, Folder_4, Folder_5.
By using "imageDatastore" function, I want to make a loop in order to chose folders one by one, something like this:
for i = 1: 5
%in the 1st loop when i = 1
Group_A = Folder_1
Group_B = Folder_2, Folder_3, Folder_4, Folder_5
%in the 2nd loop when i = 2
Group_A = Folder_2
Group_B = Folder_1, Folder_3, Folder_4, Folder_5
%in the 3rd loop when i = 3
Group_A = Folder_3
Group_B = Folder_1, Folder_2, Folder_4, Folder_5
and so on.
Any idea how to make such a loop?
best regards,
Mesho

Accepted Answer

Sai Pavan
Sai Pavan on 29 May 2024
Hello,
I understand that you have a "Parent_Folder" and want to have five sets of "Group_A" and "Group_B" subfolder groups such that "Group_A" has one subfolder and "Group_B" has the rest of the four folders to store the images inside them in a "imageDatastore" with the help of their paths.
Please refer to the below code snippet that illustrates this task:
parentFolderPath = 'Parent_Folder';
subfolders = {'Folder_1', 'Folder_2', 'Folder_3', 'Folder_4', 'Folder_5'}; % List of subfolder names
for i = 1:length(subfolders)
% Group_A will be the current subfolder
groupAPath = fullfile(parentFolderPath, subfolders{i});
Group_A = imageDatastore(groupAPath, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
% Group_B will be all other subfolders
groupBPaths = subfolders([1:i-1, i+1:end]); % Exclude current folder
groupBPaths = fullfile(parentFolderPath, groupBPaths); % Full paths
Group_B = imageDatastore(groupBPaths, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
end
Please refer to the below documentation to learn more about:
Hope it helps!

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!