- 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 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 11 Oct 2024
Edited: Stephen23
on 11 Oct 2024
"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.
Accepted Answer
Voss
on 11 Oct 2024
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.
More Answers (2)
Walter Roberson
on 11 Oct 2024
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 Comments
Steven Lord
on 11 Oct 2024
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 Comments
See Also
Categories
Find more on Search Path in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!