Folder listing with dir on Mac
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sarah Silvère
le 18 Avr 2022
Modifié(e) : Stephen23
le 19 Avr 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 commentaires
Stephen23
le 18 Avr 2022
Modifié(e) : Stephen23
le 19 Avr 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
le 18 Avr 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 ..
Réponse acceptée
Walter Roberson
le 18 Avr 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!