Addressing folder with varying name of in a path
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello, In below code:
s_root='C:\datasets\dataset_Experiment1\segments_all\Asphyxia\SHL-M-F-
0293\file';
s_infants=dir([s_root '*']);
.
.
.
The folders of Asphyxia and SHL-M-F-0293 will have different name in each run. How should I address the varying name of these folders which are within the path? Thanks
0 comentarios
Respuestas (2)
Jos (10584)
el 12 de Jul. de 2017
Take a look at the function FULLFILE. This may get you started.
folders = {'AAA','BBB'} % brows for files
for k=1:numel(folders),
CurrentFolder = fullfile('C:','basefolder',folders{k},'data')
FileStr = fullfile(CurrentFolder,'*.txt')
dir (FileStr)
end
Jan
el 12 de Jul. de 2017
Editada: Jan
el 12 de Jul. de 2017
With modern Matlab versions:
pattern = ['C:\datasets\dataset_Experiment1\segments_all\**\file';
FileList = dir(pattern);
FileName = fullfile({FileList.folder}, {FileList.name});
Afterwards you can use regexp to apply the pattern '\*Asphyxia*\*SHL-M-F-0293*\'.
I cannot test this currently due to the lack of a modern Matlab version.
[EDITED] There are many recursive dir versions in the FileExchange: https://www.mathworks.com/matlabcentral/fileexchange/?utf8=%E2%9C%93&term=recursive+dir, bu no one helps to resolve pattern like:
'C:\datasets\*Asphyxia*\*SHL-M-F-0293*\file\*.*'
But it is not hard to implement this:
Pattern = 'C:\datasets\*Asphyxia*\*SHL-M-F-0293*\file\*.*';
Pattern = strrep(Pattern, '/', '\'); % Consider unix
PatternC = splitstr(Pattern, '\');
FileList = {''};
for iP = 1:numel(PatternC)
part = PatternC{iP}
if any(part == '*') || any(part == '?')
newFileList = cell(1, numel(FileList));
for iFile = 1:numel(FileList)
DirList = dir(fullfile(FileList{iFile}, part));
if ~isempty(DirList)
newFileList{iFile} = fullfile({DirList.folder}, {DirList.name});
end
end
FileList = cat(2, newFileList{:});
else % No pattern, append string:
FileList = fullfile(FileList, part));
end
end
UNTESTED! I do not have a modern Matlab currently and cannot test this!!!
5 comentarios
Jan
el 14 de Jul. de 2017
If the part "Asphyxia" is changed, you need to find out, how you can identify the new folder. If the "segments_all" folder contains an arbitrary number of folders and you do not have any additional information about which folder you should choose, then your problem cannot be solved. The same for "SHL-M-F-0293". Please explain how you would choose the wanted folder manually. Perhaps it is the newest folder, or the only folder. Currently you did not give us enough information to solve the problem.
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!