Excluding folders from a dir Structure
52 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Mehri Mehrnia
el 31 de Mayo de 2022
I was using d(~ismember({d.name},{'.','..'})) but now ".DS" folders are added to directories and this one doesn't remove it!
Stephen23
el 31 de Mayo de 2022
@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.
Respuestas (1)
Star Strider
el 23 de En. de 2021
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 comentarios
Star Strider
el 17 de Feb. de 2021
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.
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!