Dir function only works intermittently?

I'm running a large script which involves linking an MRI voxel with an image. It's run in a long loop. I'm running MATLAB2016b. My code is here:
MaskIndex=find(MaskSlice>0);
for k=1:length(MaskIndex)
[X, Y]=ind2sub(size(MaskSlice),MaskIndex(k));
Xval=round(Xmap(MaskIndex(k)));
Yval=round(Ymap(MaskIndex(k)));
% Find the image
D=dir(fullfile(RadPath,'Data400',sprintf('Pt_%s_Slide_%s_X_%i_Y_%i_*',num2str(PtList(i)),CurrentSlide,Xval,Yval)));
if length(D)>0
Pt(k)=PtList(i);
Im{k}=D(1).name;
Z=Slice;
Coords(k,:)=[X,Y,Z];
for l=1:length(FileOrder);
MRIval(k,l)=MRI(l).vol(X,Y,Z);
end
else
disp('here');
end
The problem that's occurring is D always has a length of 0, but when I debug at disp('here') and re-run the D=dir line it comes back with a length = 1 and the if statement will run. MaskIndex, X, Y, Xval, and Yval are all variables that exist and have reasonable values. Because I can make the if statement run from debug mode but not while the program is actually looping the error is difficult to reproduce and debug. Does anyone know what's going on here? My gut is saying that the folder (Data400) where I'm searching for images from is too large, it currently contains nearly 40,000 images.

1 Commento

To be sure that it's not a bug in your code, I would separate the fullfile from the dir and store that path into a variable. You can then show the content of that path in the else:
path = fullfile(RadPath,'Data400',sprintf('Pt_%s_Slide_%s_X_%i_Y_%i_*',num2str(PtList(i)),CurrentSlide,Xval,Yval));
D = dir(path);
if ~isempty(D)
%...
else
fprintf('Failed to find anything in %s\n', path);
end
Even better, I would actually extract the whole Xval, Yval and path generation from the loop:
MaskIndex = find(MaskSlice > 0);
[X, Y] = ind2sub(size(MaskSlice), MaskIndex);
Xval = round(Xmap(MaskIndex));
Yval = round(Ymap(MaskIndex));
folders = compose('Pt_%s_Slide_%s_X_%i_Y_%i_*', num2str(PtList(i)), CurrentSlide, Xval, Yval);
paths = fullfile(RadPath, 'Data400', folders);
for k = 1:numel(paths)
D = dir(folder{k});
%...

Accedi per commentare.

Risposte (2)

It shouldn't come up as empty if there are files there. Chances are your path is wrong. So break it up into smaller parts than the horribly complicated line of code you have now. So instead of
D=dir(fullfile(RadPath,'Data400',sprintf('Pt_%s_Slide_%s_X_%i_Y_%i_*',num2str(PtList(i)),CurrentSlide,Xval,Yval)));
do this:
baseFileName = sprintf('Pt_%s_Slide_%s_X_%i_Y_%i_*',num2str(PtList(i)),CurrentSlide,Xval,Yval);
filePattern = fullfile(RadPath,'Data400',baseFileName);
fprintf('Getting files according to pattern: %s\n', filePattern);
D = dir(filePattern)
Set a break point after this and see what gets printed in the command window.
I notice you don't have an extension in your file pattern. Perhaps adding a .* to the end of the file pattern may help.
baseFileName = sprintf('Pt_%s_Slide_%s_X_%i_Y_%i_*.*',num2str(PtList(i)),CurrentSlide,Xval,Yval);
Or perhaps you have some spaces or zeros before your numbers, like " 6" or "06" instead of just "6" so now the file pattern doesn't match the actual filenames.
This sounds magic. Matlab is a deterministic programming language, which is not too lazy to read 40'000 files when it is instructed to do so. There is another problem. Sure.
Folder = fullfile(RadPath, 'Data400');
Pattern = sprintf('Pt_%d_Slide_%s_X_%i_Y_%i_*', PtList(i), CurrentSlide, Xval, Yval);
D = dir(fullfile(Folder, Pattern));
if ~isempty(D)
...
else
disp(Folder);
disp(Pattern);
disp('here');
end
Perhaps you had redefined the function "length"?
which length -all
Use the debugger for further examinations: set a breakpoint in the D=... line and descend into each subfunction.

Categorie

Prodotti

Tag

Richiesto:

il 7 Lug 2017

Commentato:

il 7 Lug 2017

Community Treasure Hunt

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

Start Hunting!

Translated by