Addressing folder with varying name of in a path
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Jos (10584)
2017-7-12
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
2017-7-12
编辑:Jan
2017-7-12
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 个评论
Jan
2017-7-14
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!