Clear Filters
Clear Filters

How to adjust the order of file sorting?

4 views (last 30 days)
ahmed on 24 Sep 2013
Edited: Stephen23 on 18 Apr 2021
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 = {};
result = [];
for n = 1:length(allFN)
measNr(n) = str2double(regexprep(allFN{n},'**.txt',''));
myFN = (fullfile(dataDirectory, allFN{n}));
auxv =try1(myFN);
result(n,:) = mean(;
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
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
Jan on 24 Sep 2013
@ahmed: If you are not sure about a command, simply look in its help text:
help sprintf
Stephen23 on 27 May 2015
Edited: Stephen23 on 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');
ans =
ans =
ans =
>> S = natsortfiles(S); % alphanumeric sort by filename
ans =
ans =
ans =

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 24 Sep 2013


Find more on Shifting and Sorting Matrices 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!