필터 지우기
필터 지우기

Addressing folder with varying name of in a path

조회 수: 2 (최근 30일)
Shaya s
Shaya s 2017년 7월 11일
편집: Shaya s 2017년 7월 14일
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