Error using imfinfo when using nested loop, but not when using single loop to process same files
21 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lina Koronfel
el 16 de Ag. de 2020
Comentada: Stephen23
el 17 de Ag. de 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 comentario
Respuesta aceptada
Walter Roberson
el 16 de Ag. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!