Clear Filters
Clear Filters

Addressing folder with varying name of in a path

2 views (last 30 days)
Shaya s
Shaya s on 11 Jul 2017
Edited: Shaya s on 14 Jul 2017
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

Answers (2)

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

Sign in to comment.


Jan
Jan on 12 Jul 2017
Edited: Jan on 12 Jul 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 Comments
Jan
Jan on 14 Jul 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.
Shaya s
Shaya s on 14 Jul 2017
Edited: Shaya s on 14 Jul 2017
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

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!