Folder listing with dir on Mac

조회 수: 13 (최근 30일)
Sarah Silvère
Sarah Silvère 2022년 4월 18일
편집: Stephen23 2022년 4월 19일
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
Stephen23
Stephen23 2022년 4월 18일
편집: Stephen23 2022년 4월 19일
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 2022년 4월 18일
. 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 ..

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2022년 4월 18일
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개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by