Error using imfinfo when using nested loop, but not when using single loop to process same files
1 view (last 30 days)
Show older comments
Lina Koronfel
on 16 Aug 2020
Commented: Stephen23
on 17 Aug 2020
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!!!
1 Comment
Accepted Answer
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
More Answers (0)
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!