Excluding folders from a dir Structure

I was wondering about excluding folders from a dir struct that contain the words 'output'
I understand how to exclude the first couple of entries in the struct
d = dir(pathName);
d = d(~ismember({d.name},{'.','..'}));
but is there a way that I could maybe use regexpi() inorder to find folders that contain the key world then insert them into the second line above?

2 件のコメント

Mehri Mehrnia
Mehri Mehrnia 2022 年 5 月 31 日
I was using d(~ismember({d.name},{'.','..'})) but now ".DS" folders are added to directories and this one doesn't remove it!
Stephen23
Stephen23 2022 年 5 月 31 日
@Mehri Mehrnia: you could either remove all structure elements whose names start with '.':
d = d(~startsWith({d.name},'.'));
or specify the DIR match string to match the required file/folder names.

サインインしてコメントする。

回答 (1 件)

Star Strider
Star Strider 2021 年 1 月 23 日

0 投票

Try this:
valid_filenames = cellfun(@isfile,{d.name}); % Eliminate ‘.’, ‘..’
dnew = d(valid_filenames); % Without ‘.’, ‘..’
idx = cellfun(@(x)contains(x,'output'),{dnew.name}, 'Unif',0); % True If ‘output’ Present
dnew_edited = dnew(~[idx{:}]); % Files Without ‘output’ In File Names
dnames = {dnew_edited.name}'; % File Names Without ‘output’
This is deliberately not as efficient as it might be (some of these could be combined), in order to demonstrate all the intermediate steps, and to provide flexibility in the event other restrictions may be necessary.

6 件のコメント

Boris Kabistan
Boris Kabistan 2021 年 1 月 24 日
I was trying this:
pathStruct = dir(pathName);
excludedNames = regexpi({pathStruct.name},'output\w*','match');
excludedNames = excludedNames(~cellfun('isempty',excludedNames));
excludedNames = cellstr(excludedNames)
pathStruct = pathStruct(~ismember({pathStruct.name}, {'.', '..',excludedNames{:}}));
Where I was trying to print out the contents of the excludedNames cells as a delimited by commas strings that would be accepted in the last line. The hurtles I was running into was cellarrays being funky to work with for me.
Star Strider
Star Strider 2021 年 1 月 24 日
Noted.
My code does what you originally stated that you want. (I tested it on one of my own directories that has files with ‘output’ in the file names, and it produced the correct result when I ran it.)
I wrote my code using R2020b, Update 4. If you are using a sufficiently earlier release/version, imy code may not work.
Boris Kabistan
Boris Kabistan 2021 年 2 月 17 日
I was able to run the code with a newer release 2019a. There is an issue where it changes the datastructure of the directory structure array into a cell array and fails to populate it. This is what I did with your help:
pathStruct = dir(pathName);
validFolders = pathStruct(cellfun(@isfile,{pathStruct.name}));
idx = cellfun(@(x)contains(x,'output'),{validFolders.name}, 'Unif',0);
validFolders = validFolders(~[idx{:}]);
pathStruct = {validFolders.name}';
Star Strider
Star Strider 2021 年 2 月 17 日
Thank you.
I no longer have access to R2019a (and I prefer not to download it, since I almost never need to test code with it).
If my Answer helped to solve your problem, please Accept it!
Boris Kabistan
Boris Kabistan 2021 年 2 月 17 日
oh sorry, by running it I meant it ran with no errors. But nothing shows up on the matricies when I test it with parent folders.
Star Strider
Star Strider 2021 年 2 月 17 日
When I ran my version (that I posted), it ran without error and produced the correct result.
The problem may be with your paths and directories. I do not have access to it, so I cannot test my code with it.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeShare and Distribute Software についてさらに検索

製品

質問済み:

2021 年 1 月 23 日

コメント済み:

2022 年 5 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by