- 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!