Now I am in a script under "C:\User\Jason\MATLAB". In this script, I want to count how many subfolder in another directory 'C:\User\Jason\MATLAB\Days'. Now I write the code in the below to count, but only return 17, actually the subfolder is more than 100. Any solution to fix this bug? Thanks.
dir C:\User\Jason\MATLAB\Days'
length(dir)-2

 채택된 답변

Guillaume
Guillaume 2016년 3월 9일

0 개 추천

You don't assign the output of the first line to anything, so it's a wasted lines. The second line just call dir again but since you don't specify a directory, does it in the current directory (which, it looks like, has 17 files/folders).
Avoid using the command form of functions in scripts. Here is a clean way of doing it:
path = 'C:\User\Jason\MATLAB\Days';
dircontent = dir(path);
numfolders = sum([dircontent.isdir]) - 2; %-2 to account for the stupid '.' and '..' returned by dir

댓글 수: 3

In your third line of code, Can I use
length(dircontent)-2
Is there any different with your third line of code with my length(dircontent)-2?
sum([dircontent.isdir])
only counts the subfolders, and ignores any files.
Jason
Jason 2016년 3월 11일
thanks

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

질문:

2016년 3월 9일

댓글:

2016년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by