list names in an array

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 comentarios

Adam Danz
Adam Danz el 13 de Jun. de 2019
Any problems using dir()?
Barakat Ibrhim
Barakat Ibrhim el 13 de Jun. de 2019
it gives the name only like C100
not the whole name
Adam Danz
Adam Danz el 13 de Jun. de 2019
It sounds like you're doing something wrong.
Does your code look like this?
d = dir('C:\Users\name\Documents\MATLAB\');
% list all file
{d.name}'
the cyclist
the cyclist el 13 de Jun. de 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 el 13 de Jun. de 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.

Iniciar sesión para comentar.

Respuestas (2)

Guillaume
Guillaume el 13 de Jun. de 2019

2 votos

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 comentarios

Adam Danz
Adam Danz el 13 de Jun. de 2019
Editada: Adam Danz el 13 de Jun. de 2019
That is more simpler to read and more intuitive. However, for those obsessed with speed, The Cyclist's solution is 1.56x faster and my solution is 1.84x faster; probably due to avoiding overhead in fullfile (median speed comparison of 10,000 repetitions of each single line solution).
Guillaume
Guillaume el 14 de Jun. de 2019
I would argue that clarity completely trumps speed in this case particularly as the speed difference would be insignificant compared to the file IO that will inevitably follow.
Stephen23
Stephen23 el 14 de Jun. de 2019
Editada: Stephen23 el 14 de Jun. de 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 el 14 de Jun. de 2019
Definitely clairity over milliseconds.

Iniciar sesión para comentar.

the cyclist
the cyclist el 13 de Jun. de 2019

1 voto

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 comentarios

Barakat Ibrhim
Barakat Ibrhim el 13 de Jun. de 2019
the output is a cell array
so how can i convert it to a string array not char array
matlab 2016
the cyclist
the cyclist el 13 de Jun. de 2019
Editada: the cyclist el 13 de Jun. de 2019
string(fileList)
will convert the cell array to a string array (at least in R2018b).
Adam Danz
Adam Danz el 13 de Jun. de 2019
"I'm pretty sure there is a better, simpler way..."
s = dir;
fileList = strcat({s.folder}',repmat({'/'},size(s)),{s.name}');
Stephen23
Stephen23 el 14 de Jun. de 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});

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 13 de Jun. de 2019

Comentada:

el 14 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by