Folder names sorting issues after reading from directory

9 Ansichten (letzte 30 Tage)
Hi, I used the following code to read all the folders based on the given directory. The issue is that the sorting order shown in figure 1 is not the same as the ones in my explorer window. The folders in the explorer window are sorted based on 'Name'. Based on my file order, the folder in Field 6 should be placed at Field 3 instead.
Question: Is the sorting order performed by Matlab based on some other types??
dinfo = dir('C:\Example');
dinfo(ismember({dinfo.name}, {'.', '..'})) = [];
Figure 1
Figure 2: Folder names sorted based on 'Name'
  1 Kommentar
Stephen23
Stephen23 am 7 Okt. 2022
Bearbeitet: Stephen23 am 7 Okt. 2022
"The issue is that the sorting order shown in figure 1 is not the same as the ones in my explorer window."
The Windows File Explorer is just some application, it is not the file system nor is it the OS.
"Is the sorting order performed by Matlab based on some other types??"
In fact the File Explorer sorts filenames into an undocumented order (i.e. as you observe it it not the same order as the File System, nor is its order documented anywhere). In contrast the NTFS file system apparently sorts into character code order, and until August 2015 the DIR documentation stated "Results appear in the order returned by the operating system":
As far as I can tell, this is still the case with recent MATLAB releases.
To sort the filenames alphanumerically you can download my FEX submission NATSORTFILES and follow its documentation:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 7 Okt. 2022
I believe it's File Explorer that is sorting it differently, not MATLAB. The files in your computer are sorted somehow by the operating system - I believe it's alphanumerically. MATLAB should just get that and give you the names in the order the OS gave them to you. However, File Explorer is a program, as you know, not the Operating System itself. And in that program you can sort the files in different ways using the "View" tab. In fact you can have several File Explorers open each sorted according to different criteria. So if that were the case, which should MATLAB use? So MATLAB does not look at File Explorer or any instance of it to list the files. It just gets it from the operating system itself. So if you have your file listing in File Explorer window sorted by date or application type, don't expect MATLAB to return a file list sorted in that same way.
  1 Kommentar
Stephen23
Stephen23 am 7 Okt. 2022
Bearbeitet: Stephen23 am 7 Okt. 2022
+1 good explanation.
"I believe it's alphanumerically." : unfortunately it is not that simple because the File Explorer order also differs according to the OS locale setting, in particular the language (i.e. alphabet order, which is not the same as character code order (e.g. see swedish)) and decimal radix character / thousands delimiter. Not only that, why is '=' char(61) sorted to immediately before the digit '0' char(48) ? Or why '{', '}' and '~' are sorted before all alphanumeric characters, even though they are the last few valid ASCII characters? What about all of the 149186 characters defined in Unicode, where is the documentation for their File Explorer sort order?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 7 Okt. 2022
Bearbeitet: dpb am 7 Okt. 2022
I've never noticed a case in which dir returned a list differering in order from that returned by the OS -- what if you look at the result of dinfo BEFORE calling ismember and retrieve those from the original?
[Elided section about removing directory entries from list; forgot these were directories alone, not files OP was looking for...the silly dot and double-dot entries are a pain, certainly, and there's no super-good way to remove them or avoid them. I'm not positive about the [] operation; on reflection it won't change the original order, so it's back to...]
ADDENDUM
As @Image Analyst points out, File Explorer is NOT the OS but a viewing app and can be sorted by any field.
However, dir does not guarantee a specific order and the actual order in absence of a specific switch passed to the DIR command (or the DIRCMD environment variable being set), the returned order is that returned by the OS software API Find­First­File/Find­Next­File dynamic duo. So, that throws the Q? down to what order does that mean? The NTFS file system internally maintains directory entries in a B-tree structure, which for US-English looks approximately alphabetical. So, one normally gets that, but one should never presume they are actually sorted nor write code based on the presumption they are.
If you want them sorted by name and must have as such, then manually sort them in code, the FEX routine that does a natural language sort may be of interest in that regards.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by