Automatically select subfolders with a folder

조회 수: 3 (최근 30일)
Daniel Matthew
Daniel Matthew 2018년 2월 1일
댓글: Walter Roberson 2018년 2월 5일
I have a script that I run to process files within a folder. This script is trigger after clicking a button on a GUI that initiates this: dataDir = uigetdir(inDataDir,'Select data directory') then runs the rest of the script. The issue is I have many of these folders and I have to run this script for each and every one of them. I was wondering if there is a way I can have the script run on every successive folder after automatically. There are also files in the root directory as well (if there was a way to explicitly select just folders, that'll be great). A line of code that kills this operation would be great too. Thanks!

채택된 답변

Walter Roberson
Walter Roberson 2018년 2월 2일
subdir_info = dir(dataDir);
subdir_info(~[subdir_info.isdir]) = []; %get rid of non folders
subdir_info(ismember({subdir_info.name}, {'.', '..'})) = []; %get rid of . and ..
subdir_names = fullfile(dataDir, {subdir_info.name}); %construct list of names
Now subdir_names is a cell array of character vectors that are the fully qualified names of the folders directly under the one that the user selected.
If you need all the nested subfolders too then see rdir() in the File Exchange, or you can do it using R2016b or later (I think it is, perhaps it was R2017a) using wildcards.
  댓글 수: 2
Daniel Matthew
Daniel Matthew 2018년 2월 5일
What do I do is there's a specific folder with a specific name I want to omit from subdir_names?
Walter Roberson
Walter Roberson 2018년 2월 5일
Add the name to the ismember() second argument.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 2월 2일
See my attached demo to recursively get filenames of all files in all subfolders under a specified folder.

카테고리

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