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

回答(2 个)

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

4 个评论

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, 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.

请先登录,再进行评论。

Here is the file I have. Thank you!

2 个评论

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.
Thank you ImageAnalyst for checking again. I use a Mac.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 File Operations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by