Azzera filtri
Azzera filtri

List only top directories

8 visualizzazioni (ultimi 30 giorni)
balandong
balandong il 17 Apr 2017
Risposto: Image Analyst il 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

Risposta accettata

balandong
balandong il 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

Più risposte (1)

Image Analyst
Image Analyst il 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.

Categorie

Scopri di più su Search Path in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by