MATLAB: How to select only 100 images in a folder of 200 images, according to a list of 100 different names

select elements in folder

Hello,
I have a list of 100 different pictures.
I have the name of the 100 pictures, for instance :
bananas
strawberrys
etc
I want to select those 100 images in the folder in which there are 200 images in total.
I can do this by hand, but does anyones have an idea of code to do so ?
Thanks a lot

Best Answer

Assuming you have the required image names in a text file. Refer the code below.
myFolder = 'putYourFolderPathHere';
filePattern = fullfile(myFolder, '*.tiff'); % Change the extension according to your file extension
tiffFiles = dir(filePattern);
fid = fopen('fileNameList.txt', 'rt'); % Assuming fileNameList.txt contains the names of the required images
idx = 1;
while feof(fid) == 0
tline = fgetl(fid);
fileNamesToBeInc{idx} = tline;
idx = idx+1;
end
for k = 1:length(tiffFiles)
baseFileName = tiffFiles(k).name;
for i = 1:length(fileNamesToBeInc)
if strcmp(fileNamesToBeInc{i}, baseFileName)
fullFileName = fullfile(myFolder, baseFileName);
temp{k} = imread(fullFileName);
end
end
end
imageArray = temp(~cellfun('isempty',temp)); % imageArray contains your image files
% Display all the selected images
montage(imageArray);