Structured File and Directory List Matlab

5 visualizaciones (últimos 30 días)
James Union
James Union el 9 de Jun. de 2015
Editada: per isakson el 10 de Jun. de 2015
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

Respuestas (2)

Image Analyst
Image Analyst el 9 de Jun. de 2015
You can use genpath(). See my attached demo.
  4 comentarios
Image Analyst
Image Analyst el 9 de Jun. de 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
Image Analyst el 10 de Jun. de 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.

Iniciar sesión para comentar.


James Union
James Union el 9 de Jun. de 2015
Here is the file I have. Thank you!
  2 comentarios
Image Analyst
Image Analyst el 9 de Jun. de 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 el 9 de Jun. de 2015
Thank you ImageAnalyst for checking again. I use a Mac.

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by