How to adjust the order of file sorting?

I am trying to evalute my Lab data, what are represtned into TXT files then plot the conntents of data.
dataDirectory = 'files location ';
allFilesDir = dir(fullfile(dataDirectory , '*.txt'));
allFN = {allFilesDir.name};
result = [];
for n = 1:length(allFN)
measNr(n) = str2double(regexprep(allFN{n},'**.txt',''));
myFN = (fullfile(dataDirectory, allFN{n}));
auxv =try1(myFN);
result(n,:) = mean(auxv.data);
end
This code is workin but into wrong order arrangemnt the TXT files (0,1,11,12,13,14 .....) which give me wrong plot data. I used
plot(measNr,resultMG(1:35,2),'x');
but i read that i can use sort_nat in this way but it did not work for me .
% [~, order] = sort_nat({result});
% result = result(order);
and I got this error messge
:??? Undefined function or method 'sort_nat' for input arguments of type
'cell'.:

5 Kommentare

dpb
dpb am 24 Sep. 2013
No sort_nat here, sorry...
I'd suggest creating the file names using sotoo
fn=sprintf('file%02d.txt',n);
and then sort will produce them in numerical order. Plus, it's just convenient for the filenames to all have same length for pretty listing on occasion.
ahmed
ahmed am 24 Sep. 2013
But I think sprint is used to create files. My data already existed . Am I wrong in this ?
Walter Roberson
Walter Roberson am 24 Sep. 2013
sprintf() is print to string, not create files.
@ahmed: If you are not sure about a command, simply look in its help text:
help sprintf
Stephen23
Stephen23 am 27 Mai 2015
Bearbeitet: Stephen23 am 18 Apr. 2021
A simple solution is to use my FEX submission natsortfiles
It sorts according to any numeric values in the strings, and also sorts the file extensions separately:
>> S = dir('*.txt');
>> S.name
ans =
'1.txt'
ans =
'10.txt'
ans =
'2.txt'
>> S = natsortfiles(S); % alphanumeric sort by filename
>> S.name
ans =
'1.txt'
ans =
'2.txt'
ans =
'10.txt'

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

am 24 Sep. 2013

Bearbeitet:

am 18 Apr. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by