Folder listing with dir on Mac

13 visualizzazioni (ultimi 30 giorni)
Sarah Silvère
Sarah Silvère il 18 Apr 2022
Modificato: Stephen23 il 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 Commenti
Stephen23
Stephen23 il 18 Apr 2022
Modificato: Stephen23 il 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
Walter Roberson il 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 ..

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 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});

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by