Filter löschen
Filter löschen

List only top directories

8 Ansichten (letzte 30 Tage)
balandong
balandong am 17 Apr. 2017
Beantwortet: Image Analyst am 17 Apr. 2017
Dear Matlab_User
May I know the best approach to list down only the first level directories.
For example, I have a dir with subfolder inside, such as
C:\Users\1st_level_a\2nd_level
C:\Users\1st_level_b\2nd_level
C:\Users\1st_level_c\2nd_level\3rd_level.
However, I am only interested to obtain the url name for the 1st_level,
C:\Users\1st_level_a
C:\Users\1st_level_b
C:\Users\1st_level_c
I tried using the genpath (), however, genpath return me the sub dir as well.
Any suggestion is most welcome,
Cheers,
Rodney

Akzeptierte Antwort

balandong
balandong am 17 Apr. 2017
I found a dirty workaround, but I welcome if there are any more efficient script to replace my method, thansk
start_path = fullfile(matlabroot, 'C:\Users\');
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
list=dir(topLevelFolder); %get info of files/folders in current directory
dirnames={list([list.isdir]).name};
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames)));
for i = 1:length (dirnames)
s (i) = strcat (topLevelFolder,'\', dirnames (i))
end

Weitere Antworten (1)

Image Analyst
Image Analyst am 17 Apr. 2017
Try this:
startingFolder = 'C:/windows/media';
files = dir(startingFolder)
folderIndexes = [files.isdir]
folders = {files(folderIndexes).name} % Note, first two are . and ..
folders = folders(3:end) % Ignore . and .., if you want.

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by