Structured File and Directory List Matlab

I have a main folder with n number of directories. In each of these n directories I have further subdirectory listing m. In each of these m folder I have some images which I want to do stuff with.
The plan is go to each of the m folder do stuff to all the images and store results in that folder with the same as the existing file. So that if there is a file foo.tif, the results is foo.mat, foo.txt or foo.*
This suggest that I should recursively traverse the folders do stuff, pick another folder do stuff and so on.
This might be the silly way for an ad hoc Matlab programmer, but I decided to make an struct array of the folder structure, access the name by indexing. Finally, use the names accessed by indexing for locations. Use imread or something for the constructed path, then do stuff.
I cannot construct the struct array I want. Please see the code and let's know what you think.
Thanks!
function [dirNames,ImagesFoldLoc,fileNames,files,dirInfo] = processDirectory(path)
while ~strcmp(path(end), filesep)
path(end+1)=filesep;
end
dirInfo= dir(path);
files=~[dirInfo.isdir];
fileNames={dirInfo(files).name};
disp(path)
isDir=[dirInfo.isdir];
dirNames={dirInfo(isDir).name};
dirNames(strcmp(dirNames, '.') | strcmp(dirNames, '..'))=[];
MainImgLoc.Folders=dirNames;
ImagesFoldLoc=MainImgLoc.Folders;
if ~isempty(fileNames)
for i=1:length(fileNames)
end
end
for i=1:length(dirNames)
processDirectory([path dirNames{i} filesep]);
%disp(dirNames)
end

Réponses (2)

Image Analyst
Image Analyst le 9 Juin 2015

0 votes

You can use genpath(). See my attached demo.

4 commentaires

James Union
James Union le 9 Juin 2015
Modifié(e) : per isakson le 10 Juin 2015
I get this error from the demo:
Error using dir
Pathname
'/Users/Junred/Desktop/Junred_TestFolder/Fold1:/Users/Junred/Desktop/Junred_TestFolder/Fold1:/Users/Junred/Desktop/Junred_TestFolder/Fold1:/Users/Junred/Desktop/Junred_TestFolder/Fold1:/Users/Junred/Desktop/Junred_TestFolder/Fold1:/Users/Junred/Desktop/Junred_TestFolder/Fold1:…
is too long.
Error in recurse_subfolders (line 43)
baseFileNames = dir(filePattern);
James Union
James Union le 9 Juin 2015
Will keep looking...
Image Analyst
Image Analyst le 9 Juin 2015
James, you must have changed "thisFolder" to something other than what I had. Please attach your complete script with the paper clip icon so I can fix it.
I don't use a Mac so I'm guessing. Put these lines before the for loop so we can see what they spew out to the command window:
allSubFolders
topLevelFolder
Come back here and copy and paste what it reported to the command window.

Connectez-vous pour commenter.

James Union
James Union le 9 Juin 2015

0 votes

Here is the file I have. Thank you!

2 commentaires

Image Analyst
Image Analyst le 9 Juin 2015
It worked fine for me. What is the name of the folder you chose? What I can't figure out is why you don't have a drive letter in what you posted. What operating system are you using? I've tested this on Windows.
James Union
James Union le 9 Juin 2015
Thank you ImageAnalyst for checking again. I use a Mac.

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Produits

Modifié(e) :

le 10 Juin 2015

Community Treasure Hunt

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

Start Hunting!

Translated by