Folder listing with dir on Mac
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sarah Silvère
am 18 Apr. 2022
Bearbeitet: Stephen23
am 19 Apr. 2022
Hello,
The code I would like to run on my personal laptop (Mac) is not working, whilst it is on PC.
The problem is : I want to list all the folders' names from a directory into a matrix (26x15 char) = the names are aligned in column. But on Mac, it aligns the names on a row (and then the rest of my code doesn't work anymore).
The MATLAB version on my Mac is the 2020a whereas the one on the PC is from 2018b
Is there any explication to this difference?
dir=ls('Data_from_the_directory/');
dir(1:2,:)=[];
2 Kommentare
Stephen23
am 18 Apr. 2022
Bearbeitet: Stephen23
am 19 Apr. 2022
Rather than using LS, the best approach is to use DIR to return a structure of the folder contents.
"Is there any explication to this difference?"
Assuming that the first two elements are dot directory names is fragile/buggy and counter-productive. You should remove the dot directories by matching their names, not by assuming anything about their position (depends on the folder/filenames) or even their existence (depends on the search string). When you remove them by name then your code will also work on any OS and for any search string, unlike the anti-pattern approach you are attempting now.
Walter Roberson
am 18 Apr. 2022
. and .. definitely exist in Unix and Linux and MacOS.
I am having difficulty finding out whether Unix originated the use of them. Unix derived from Multics, which is said to be the first operating system with hierarchical directories. The documentation I find for Multics suggests that it used colon as the path separator and if I interpret correctly used * for parent directory, so Unix might plausibly have invented using . and ..
Akzeptierte Antwort
Walter Roberson
am 18 Apr. 2022
dir=ls('Data_from_the_directory/');
dir(1:2,:)=[];
That code is wrong on every operating system that MATLAB has ever run on. You should be using
dinfo = dir('Data_from_the_directory/');
dinfo(ismember({dinfo.name}, {'.', '..'})) = [];
folder_names = char({dinfo([dinfo.isdir]).name});
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!