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

1 Ansicht (letzte 30 Tage)
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!!!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentare
Lina Koronfel
Lina Koronfel am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by