Info

This question is closed. Reopen it to edit or answer.

How do I allow user to choose either a folder or an image(or selective images) to run the rest of the functions?

1 view (last 30 days)
Currently it is able to select a folder but I want the users to have other options if they want to select an image/ selective images instead.
%% ACCESS ALL IMAGES IN THE FOLDER
selpath = uigetdir;
imageFolder = dir(fullfile(selpath,'\*.jpg'));
numfiles = length(imageFolder);
ori_roi = cell(numfiles, 1);
i = 1;
%% LOOP THROUGH ALL IMAGES IN THE FOLDER & RUN ALL THE 5 FUNCTIONS
for i=1:length(imageFolder)
tic
functionError = 0;
filename = fullfile(selpath,imageFolder(i).name);
originalImage = imread(filename);
fprintf("Image name: %s\n",imageFolder(i).name);
...
  3 Comments
Shu Yi Ho
Shu Yi Ho on 15 Aug 2019
Thanks for the reply but it doesn't work for my case. Unable to see the individual images when i clicked on the folder.
Geoff Hayes
Geoff Hayes on 16 Aug 2019
Shu - please clarify what you mean by unable to see the individual images. Where are you expecting to see them? Are you not seeing the files or are you expecting to see the actual image?

Answers (1)

Walter Roberson
Walter Roberson on 16 Aug 2019
choice = menu('How do you want to select images?', 'by folder', 'individual image');
switch choice
case 0
%user cancelled
return
case 1
%% ACCESS ALL IMAGES IN THE FOLDER
selpath = uigetdir;
if ~ischar(selpath)
%user cancelled
return
end
imageFolder = dir(fullfile(selpath,'*.jpg'));
filenames = fullfile(selpath, {imageFolder.name});
case 2
%% ACCESS ONE IMAGE IN A FOLDER
[selfile, selpath] = uigetfile({'*.jpg', '*.png', '*.tif', '*.*'}, 'Pick a file');
if ~ischar(selfile)
%user cancelled
return
end
filenames = {fullfile(selpath, selfile)};
otherwise
error('somehow choice is out of range??')
end
numfiles = length(filenames);
ori_roi = cell(numfiles, 1);
%% LOOP THROUGH ALL IMAGES IN THE FOLDER & RUN ALL THE 5 FUNCTIONS
for i=1:numfiles
tic
functionError = 0;
filename = filenames{i};
[~, basename, ~] = filepaths(filename);
originalImage = imread(filename);
fprintf("Image name: %s\n",basename);
...
ori_roi{i} = ....
toc
end

Community Treasure Hunt

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

Start Hunting!