Error using imfinfo when using nested loop, but not when using single loop to process same files

1 view (last 30 days)
The imfinfo function is working perfectly when I process files in single folder. For example using the code given here where the matlab file is in the same folder as the files being processed:
baseFileNames=dir('*.tif');
fileinfo1=imfinfo(sprintf('%s',baseFileNames(1).name));
W=fileinfo1.Width;
H=fileinfo1.Height;
n=length(baseFileNames);
F=n.*5;
However, when I try to apply the same code on multiple subfolders, I get an error at the fileinfo1 step that it can't read the first file in the folder:
""Error using imfinfo (line 142)
Unable to open file "B12N1_EBC_CaRec_session4_FullFramImaging_001.tif" for reading."
The code I use is as following:
start_path = fullfile(cd);
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
%.............Process all image files in those folders...................
for k = 1 : numberOfFolders;
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
if strfind(thisFolder, 'Frame')
baseFileNames = dir(sprintf('%s/*.tif', thisFolder));
fileinfo1=imfinfo(sprintf('%s',baseFileNames(1).name));
W=fileinfo1.Width;
H=fileinfo1.Height;
n=length(baseFileNames);
F=n.*5;
%Rest of code
end
end
baseFileNames give exactly the same output in both conditions, however, the fileinfo1 is only giving output in the first case. Please help!!!

Accepted Answer

Walter Roberson
Walter Roberson on 16 Aug 2020
start_path = fullfile(cd);
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
dinfo = dir( fullfile(topLevelFolder, '**', '*Frame*') ); %all *Frame* folders
dinfo(~[dinfo.isdir]) = []; %remove non-folders
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and ..
listOfFolderNames = fullfile({dinfo.folder}, {dinfo.name});
numberOfFolders = length(listOfFolderNames);
for k = 1 : numberOfFolders;
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
tinfo = dir(fullfile(thisFolder, '*.tif'));
listOfFileNames = fullfile({tinfo.folder}, {tinfo.name});
numberOfFiles = length(listOfFileNames);
for N = 1 : numberOfFiles
thisfile = listOfFileNames{N};
fileinfo1 = imfinfo(thisfile);
W = fileinfo1.Width;
H = fileinfo1.Height;
n = numberOfFiles; %hard to see why you would want this
F = n.*5;
%other code
end
end
  2 Comments
Lina Koronfel
Lina Koronfel on 17 Aug 2020
Thanks alot!! I did some modifications using your recommendations and now it is working perfect. However, I'm curious as whether these two commands are different? Since my understanding is that they give similar output:
dir(fullfile(thisFolder, '*.tif')); % Your recommendation
dir(sprintf('%s/*.tif', thisFolder)); % What I used earlier

Sign in to comment.

More Answers (0)

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!