list names in an array

조회 수: 17 (최근 30일)
Barakat Ibrhim
Barakat Ibrhim 2019년 6월 13일
댓글: Adam Danz 2019년 6월 14일
I have many files that i want to list their names in an array but the name is full for example
F:\New\checkfiles\C100.csv
  댓글 수: 5
the cyclist
the cyclist 2019년 6월 13일
It would be handy if dir took an argument, allowing specification of the full path in the output. It seems like this would be a common enough thing to want to do.
Adam Danz
Adam Danz 2019년 6월 13일
Agreed. I suggested testing d.name because the OP stated that it was only returning file name when it should be returning the file extension as well.

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

답변 (2개)

Guillaume
Guillaume 2019년 6월 13일
A lot simpler than all that has been suggested:
foldercontent = dir('C:\somewhere\*.csv');
filelist = fullfile({foldercontent.folder}, {foldercontent.name}); %that's all that is needed.
As said, if a string array is needed, string will convert the cell array of char vectors into a string array
  댓글 수: 4
Stephen23
Stephen23 2019년 6월 14일
편집: Stephen23 2019년 6월 14일
+1 fullfile is definitely the way to go.
It is highly unlikely that constructing filenames is going to be a bottleneck in the code.
Adam Danz
Adam Danz 2019년 6월 14일
Definitely clairity over milliseconds.

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


the cyclist
the cyclist 2019년 6월 13일
I'm pretty sure there is a better, simpler way to do this, but I believe this does what you want:
s = dir;
fileList = cellfun(@(x,y)[x,'/',y],{s.folder}',{s.name}','UniformOutput',false);
  댓글 수: 4
Adam Danz
Adam Danz 2019년 6월 13일
"I'm pretty sure there is a better, simpler way..."
s = dir;
fileList = strcat({s.folder}',repmat({'/'},size(s)),{s.name}');
Stephen23
Stephen23 2019년 6월 14일
"I'm pretty sure there is a better, simpler way..."
S = dir(...);
F = cellfun(@fullfile,{S.folder}',{S.name}','uni',0);
Or simply:
F = fullfile({S.folder},{S.name});

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by