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개)
Image Analyst
2015년 6월 9일
0 개 추천
You can use genpath(). See my attached demo.
댓글 수: 4
James Union
2015년 6월 9일
편집: per isakson
2015년 6월 10일
James Union
2015년 6월 9일
Image Analyst
2015년 6월 9일
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
2015년 6월 10일
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
2015년 6월 9일
0 개 추천
댓글 수: 2
Image Analyst
2015년 6월 9일
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
2015년 6월 9일
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!