Structured File and Directory List Matlab
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (2)
Image Analyst
le 9 Juin 2015
You can use genpath(). See my attached demo.
4 commentaires
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.
Image Analyst
le 10 Juin 2015
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.
James Union
le 9 Juin 2015
2 commentaires
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.
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!