Error using imfinfo when using nested loop, but not when using single loop to process same files
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2020-8-16
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 个评论
更多回答(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!