using natsortfiles to import csv files correctly
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have been looking for a way to import many csv files into matlab in the correct order (e.g. csv1, csv2, csv3).
My csv files have a naming structure of c0_1f0_1_0.csv, c0_1f0_1_1.csv etc. Where I want them ordered by the last digit.
I have found the function natsortfiles and have downloaded this from the file exchange and saved it into my working folder, but I am new to this and I am struggling to get it to work in my script from here. Is there anything I am missing or doing wrong?
P = 'E:.....';
S = dir(fullfile(P,'*.txt'));
C = natsortfiles({S.name});
for k = 1:numel(C)
fullfile(P,C{k})
end
3 comentarios
Stephen23
el 10 de Feb. de 2021
"but it says that natsort files does not exist"
All of the required files are in the downloaded zip file (you need both NATSORT and NATSORTFILES).
Respuestas (1)
Stephen23
el 10 de Feb. de 2021
Editada: Stephen23
el 18 de Abr. de 2021
Taking a guess that you actually want to match the last integer, not just the last digit:
S = natsortfiles(S,'\d+^$'); % alphanumeric sort by filename
Remove the plus if you really just want the last digit.
4 comentarios
Stephen23
el 11 de Feb. de 2021
Editada: Stephen23
el 18 de Abr. de 2021
"...but this does not rearrange the files in the structure"
NATSORTFILES will sort a cell array of filenames, but can also sort the DIR structure directly:
>> S = dir('*.txt');
>> S.name
ans =
'1.txt'
ans =
'10.txt'
ans =
'2.txt'
>> S = natsortfiles(S);
>> S.name
ans =
'1.txt'
ans =
'2.txt'
ans =
'10.txt'
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!