Using struct=dir(selpath), what do '.' and '..' mean as struct.name?
7 次查看(过去 30 天)
显示 更早的评论
When I use struct=dir(selpath) and then struct.name, I see two extra names ('.' and '..' ) that are not name of folders in the route.
0 个评论
采纳的回答
Walter Roberson
2019-10-10
It is not recommended to skip the first two entries because there are real cases where . and .. will not be the first two entries.
Instead:
dinfo = dir(selpath);
dinfo([dinfo.isdir]) = []; %get rid of all folders
or
dinfo = dir(selpath);
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %get rid of those ones
Amplification: MS Windows and Unix systems do not define a particular sort order for files returned by directory services. The order is defined by the file system driver, and historically has included "write a new file in the first available slot" unsorted. Microsoft's NTFS file system does not appear to have a publicly defined file order, but in practice appears to sort by byte value. Or perhaps it sorts by UTF-16 code point... I do not know.
MacOS's HFS+ filesystem uses 16 bit UNICODE characters "fully decomposed and in canonical order" -- but the canonical order is apparently not the right canonical order, and people who work with file internationalization apparently get pretty frustrated about HFS+ in practice.
Linux does not define a sort order, and I find postings that say that in practice the order can vary between different machines. I see one posting saying that etx4 filesystems end up sorting files according to a hash of their name. https://serverfault.com/questions/406229/ensuring-a-repeatable-directory-ordering-in-linux
The practical upshot for NTFS (Windows) and HFS+ (Mac) is that file names that begin with any of the characters !"#$%&'()*+,- or space usually sort before '.' in dir() order .
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!