How to select a folder at runtime?

조회 수: 4 (최근 30일)
Jhilam Mukherjee
Jhilam Mukherjee 2015년 9월 2일
댓글: Image Analyst 2015년 9월 2일
I have five folder. Each folder contains nearly 30 images. My programme is capable to read and execute all the images simultaneously from a specific folder by executing following code
srcFiles = dir('F:\Input_image\*.jpg');
for i = 1 : length(srcFiles)
filename = strcat('F:\Input_image\',srcFiles(i).name);
I{i} = imread(filename);
end
Each time when I execute all the folder I have to change the path, manually. Is it possible to change the path by executing the programme.

채택된 답변

Image Analyst
Image Analyst 2015년 9월 2일
Try uigetdir().
  댓글 수: 2
Jhilam Mukherjee
Jhilam Mukherjee 2015년 9월 2일
Thanks. I can select the folder at runtime. I replace the filepath in dir() command but when for loop is executed it shows length(srcFiles) is zero. Is any additional command has to be added in programme.
Image Analyst
Image Analyst 2015년 9월 2일
Try this:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the folder that the user wants to use.
folder = uigetdir(startingFolder, 'Select a folder');
if folder == 0
% User clicked the Cancel button.
return;
end
filePattern = fullfile(folder, '*.jpg')
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Read in image and display it.
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by