- Do not pollute the Search path with folders filled with data files.
- Do not change the Search Path just to access data files.
Not able to use imread on images in subfolders
조회 수: 4 (최근 30일)
이전 댓글 표시
I compiled an application for the purpose of comparing the histogram of two different images.
This is to be used in manufacturing where daily we're adding a new folder and create new images that will need to be compared.
I put the Matlab application in the parent folder but I keep getting the attached error that "Unable to find file" when I'm using uigetfile in my program to select files (which are in subfolders). I had a fix in the matlab application by using the following:
SearchPath = uigetdir('C*');
addpath(SearchPath)
It worked until I compiled the program. "addpath" does not work in Compiler.
I can't add in all these subfolders to the paths because mutliple subfolders get added everyday.
Is there anyway for imread to work on reading subfolders? Is there something I'm missing?
댓글 수: 1
Stephen23
2024년 10월 11일
편집: Stephen23
2024년 10월 11일
"Is there anyway for imread to work on reading subfolders?"
Of course: use absolute/relative filenames. FULLFILE is key here.
"Is there something I'm missing?"
The MATLAB Search Path is for code, not for data files:
Use absolute/relative filenames.
채택된 답변
Voss
2024년 10월 11일
Take the first two outputs from uigetfile (the second output is the location of the selected file), and construct the full path file name using fullfile.
Example:
[filename,pathname] = uigetfile('*_PRE DUT.tif');
if isequal(filename,0) % user cancelled
return
end
fullPreDutName = fullfile(pathname,filename);
[filename,pathname] = uigetfile('*_POST DUT.tif');
if isequal(filename,0) % user cancelled
return
end
fullPostDutName = fullfile(pathname,filename);
% ...
preDUTimage = im2uint8(imread(fullPreDutName));
% ...
postDUTimage = im2uint8(imread(fullPostDutName));
% ...
No need for addpath anywhere.
추가 답변 (2개)
Walter Roberson
2024년 10월 11일
No, there is no way to get imread() to work on reading subfolders.
You should be using roughly
SearchPath = uigetdir('C*');
if isnumeric(SearchPath)
return; %user cancel
end
dinfo = dir(SearchPath);
dinfo([dinfo.isfolder]) = []; %get rid of . and .. and other folders
files = fullfile({dinfo.folder}, {dinfo.name});
numfiles = numel(files);
all_images= cell(numfiles,1);
used_images = 0;
for K = 1 : numfiles
thisfile = files{K};
try
thisimage = imread(thisfile);
used_images = used_images + 1;
all_images{used_images} = thisimage;
catch ME
end
end
all_images = all_images(1:used_images);
댓글 수: 0
Steven Lord
2024년 10월 11일
Instead of adding the path to the file that contains your data to the MATLAB search path, construct the full path to the data files (or the directory) using fullfile. I'm setting SearchPath to a known fixed string because MATLAB Answers doesn't support uigetdir, but your application could use that instead.
SearchPath = fullfile(matlabroot, 'toolbox', 'matlab', 'general')
Now to construct the path to one of the files in that directory, say bench.dat:
benchDataPath = fullfile(SearchPath, 'bench.dat')
Can I use this directory to access the file?
lines = readmatrix(benchDataPath, Range='1:3', OutputType = "string", Delimiter="")
[The output skipped an empty line; lines(2) is actually the third line of the file.]
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Search Path에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!