list names in an array

29 views (last 30 days)
Barakat Ibrhim
Barakat Ibrhim on 13 Jun 2019
Commented: Adam Danz on 14 Jun 2019
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 Comments
the cyclist
the cyclist on 13 Jun 2019
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 on 13 Jun 2019
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.

Sign in to comment.

Answers (2)

Guillaume
Guillaume on 13 Jun 2019
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 Comments
Stephen23
Stephen23 on 14 Jun 2019
Edited: Stephen23 on 14 Jun 2019
+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 on 14 Jun 2019
Definitely clairity over milliseconds.

Sign in to comment.


the cyclist
the cyclist on 13 Jun 2019
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 Comments
Adam Danz
Adam Danz on 13 Jun 2019
"I'm pretty sure there is a better, simpler way..."
s = dir;
fileList = strcat({s.folder}',repmat({'/'},size(s)),{s.name}');
Stephen23
Stephen23 on 14 Jun 2019
"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});

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!