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

回答(2 个)

Jos (10584)
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
  1 个评论
Shaya s
Shaya s 2017-7-12
Thanks. But I don't understand what is the first line for? And it should address the folder, not .txt file.

请先登录,再进行评论。


Jan
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
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.
Shaya s
Shaya s 2017-7-14
编辑:Shaya s 2017-7-14
One had helped me which works:
dataset_seg_path='C:\dataset_tests\dataset_Experiment1\segments_all';
d = dir(dataset_seg_path);
for i=3:numel(d)
if(d(i).isdir)
dataset_classes_path = fullfile(dataset_seg_path, d(i).name);
end
end
Thank you for your attention

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by