using natsortfiles to import csv files correctly

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 Comments

You write that you want to "import many csv files", but you then write that you "want them ordered by the last digit". Of course there are only ten digits (0-9) in the decimal numbering system, so this seems a bit restrictive for sorting "many" files (where presumably "many" means more than ten).
Did you actually mean to write that you want to sort by the last number or integer? (regardless of how many digits)
Or do you really want to sort by just the last digit? (exactly as you wrote, ignoring any preceding adjacent digits)
"I am struggling to get it to work in my script from here"
What is the specific problem that you are having? The code you showed should sort the filenames into alphanumeric order (taking into account all integers in the filenames, not just the last integer).
sorry for wording my question so confusingly. I have 1000 files, so i want to sort them by the last number.
I have downloaded the zip file from the file exchange, extracted it and put it in my working folder. I am trying to use the code above but it says that natsort files does not exist, so I meant do I have to import anything else before I do this line?
"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).

Sign in to comment.

Answers (1)

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 Comments

as i said above, I get the error that natsortfiles does not exist when I run this line, even though the unzipped folder is in my workspace. do I have to run anything before I run this code you wrote to get it to work?
"do I have to run anything before I run this code you wrote to get it to work?"
Download and unzip, that is all.
Make sure that the files are on your MATLAB Search Path (the simplest is to put them in the current directory). If you put the files into another folder (as you write), the you will also need to add the new folder to the Search Path (you can do this easily using the MATLAB IDE file browser window).
Thankyou I have got this to work now I have sorted the search path.
Just another question. In the code below, S reads all the files into matlab, and then after that the names are sorted into number order saved in C, but this does not rearrange the files in the structure. If i now manipluate this data more in matlab will it continue to read the files in the correct name order?
P = 'E:...';
S = dir(fullfile(P,'*.csv'));
C = natsortfiles({S.name});
for k = 1:numel(C)
a = fullfile(P,C{k})
end
"...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'

Sign in to comment.

Categories

Asked:

on 10 Feb 2021

Edited:

on 18 Apr 2021

Community Treasure Hunt

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

Start Hunting!