필터 지우기
필터 지우기

dir() function not working

조회 수: 15 (최근 30일)
Frederikke Hansen
Frederikke Hansen 2021년 1월 17일
편집: Stephen23 2023년 7월 29일
A script I'm working with uses dir(pwd) to index the folders in the current directory and access the relevant ones. For the last few months it's been working perfectly. Now I get an error, and I've found that it stems from dir() listing the contents of the directory in a different order than the directory itself, so when the script tries to access the relevant files, they are not what I need.
folders = dir(pwd) ;
folders = folders([folders.isdir] & ~strncmpi('.', {folders.name}, 1)) ;
The directory in question lists files in alphabetical order. When dir() reads the contents, it mixes up the order of the files. I've tried to sort the filenames given by dir(), but for some reason this doesn't work either.
The strange thing is that it worked perfectly for a long time, and stopped working with no changes at all to the script or directory in question. Please help!
  댓글 수: 1
Stephen23
Stephen23 2023년 7월 29일
편집: Stephen23 2023년 7월 29일
You could use NATSORTFILES
something like this:
S = dir('.'); % simpler and more efficient than calling two functions
S = natsortfiles(S,[],'rmdot','noext');
S = S([S.isdir])

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

채택된 답변

Rik
Rik 2021년 1월 17일
It doesn't mix up the order. It sorts it, which is not what you expect. Sorting by character means that you get all upper case first, followed by lower case.
I suggest you use something like natsort (search the file exchange for it) to sort the output of dir.
Note that dir also relies on the operating system, meaning that any changes in windows might cause differences. It is not a good idea to rely on the output order.
  댓글 수: 1
Frederikke Hansen
Frederikke Hansen 2021년 1월 17일
Thank you! I will check out natsort.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by