How to use "dir"?

조회 수: 2(최근 30일)
SM
SM 2020년 9월 21일
편집: Stephen 2020년 9월 21일
I have 45 folders with name Inst1, Inst2, Inst3,Inst4,........Inst45. I use a command of
finfo=dir('Inst*');
The outcome should come in ascending order of folders but it comes random. How can I solve the problem?
  댓글 수: 1
Stephen
Stephen 2020년 9월 21일
"The outcome should come in ascending order of folders..."
I don't see it documented anywhere that any particular order "should" be returned by dir.
"...but it comes random."
Not random, most likely sorted by character order. You can sort them alphanumerically if you want:

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

답변(2개)

per isakson
per isakson 2020년 9월 21일
Renam the folders 'Inst1', to, 'Inst01', etc. or see natural order.
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 9월 21일
Renaming the folders with leading 0s on the numbers will probably work, but it is not guaranteed.
dir() is not formally defined to return any particular order: the order returned is whatever the operating system returns.
In turn, in MS Windows, Mac, and Linux, the underlying directory filesystem operation is not defined to return any particular order: the order depends upon what the filesystem layer returns.
Mac and Linux filesystems document the order of files -- though in some cases the defined order is "whatever happens to work out in the b-trees".
I have not found any formal definition of the order for NTFS file system. In practice for characters up to char(255) it sorts in order by character value. I have not seen any formal definition for NTFS sorting order for unicode characters.
POSIX formally defines sorting rules, but it also formally defines that the sorting rules are to be influenced by the user's LOCALE settings, so the order is not always the same between different users. For example in Spanish, ñ should sort after n but before o .
All of which is to say that if you need the files to be arranged in a particular order that is different from "whatever the filesystem and operating system agree on" then you should use a filename sorting routine.

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


VBBV
VBBV 2020년 9월 21일
편집: Walter Roberson 2020년 9월 21일
Type
finfo.name
Or use
finfo=dir('*Inst**');
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 9월 21일
The revised versions do not address the issue of the order of files in what dir() returns.
When files are sorted by ASCII sort order, then you have to sort from left to right. If you have
Inst1.dir
Inst2.dir
Inst10.dir
then all the 'Inst1*' have to sort before all of the 'Inst2*' because '1' sorts before 2. So, you are either going to end up with Inst1.dir Inst10.dir Inst2.dir, or else Inst10.dir Inst1.dir Inst2.dir . Which of those two you get will depend upon where '0' sorts relative to '.' . It happens that '.' sorts before '0', so you will get Inst1.dir Inst10.dir Inst2.dir .
The system is not paying any attention at all to the numeric meanings of '1', '2', and '10': it is sorting character by character.

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by