how do I open files automatically in certain order?(numbers giving me trouble)

So, say I have these files named 1.0.txt, 1.5.txt, 2.0.txt, 2.5.txt, ... 10.0.txt, 10.5.txt
I am automatically opening the files by loop and do what I have to do with the file.
However, Matlab seems to see the file order starting from the first letter.
There fore, Matlab executes the loop in the order of 1.0.txt, 1.5.txt, 10.0.txt, 10.5.txt, 2.0.txt, 2.5.txt... and so on.
I can I avoid this and make the loop go in the order of 1.0, 1.5, 2.0, 2.5, ... 10.0, 10.5 ?
I am doing fprintf and the order is important for my calculation..

 Accepted Answer

You can download my FEX submission natsortfiles to sort the files into the order you want:
S = dir(...);
S = natsortfiles(S,'\d+\.?\d*');
for k = 1:numel(S)
F = S(k).name % filename
val = sscanf(F,'%f') % numeric value
...
end
And here are your example strings:
>> X = {'1.0.txt','1.5.txt','10.0.txt','10.5.txt','2.0.txt','2.5.txt'}
X =
'1.0.txt' '1.5.txt' '10.0.txt' '10.5.txt' '2.0.txt' '2.5.txt'
>> natsortfiles(X,'\d+(\.\d+)?')
ans =
'1.0.txt' '1.5.txt' '2.0.txt' '2.5.txt' '10.0.txt' '10.5.txt'
Do you see that my code puts them into the order that you want? That is why in my last answer I told you to use natsortfiles.
EDIT If the files always contain only one decimal values, and these are always the leading characters of the filename, then this will also work:
>> X = {'1.0.txt','1.5.txt','10.0.txt','10.5.txt','2.0.txt','2.5.txt'};
>> N = cellfun(@(s)sscanf(s,'%f'),X);
>> [~,idx] = sort(N);
>> X(idx)
ans =
'1.0.txt' '1.5.txt' '2.0.txt' '2.5.txt' '10.0.txt' '10.5.txt'

4 Comments

I am sorry I am not that good with matlab.. and again, thank you for trying to help me out.
I tried your exact code as below:
S = dir(...);
N = natsortfiles({S.name},'\d+(\.\d+)?');
for k = 1:numel(N)
N{k} % filename
val = sscanf(N{k},'%f') % numeric value
...
end
X = {'1.0.txt','1.5.txt','10.0.txt','10.5.txt','2.0.txt','2.5.txt'};
natsortfiles(X,'\d+(\.\d+)?')
when I run the code, error massage comes out as:
"??? Error: File: ex2.m Line: 2 Column: 3
The expression to the left of the equals sign is not a valid target for an assignment."
I guess N = natsortfiles({S.name},'\d+(\.\d+)?'); has some problem.
Should I do something about '...'?
or is it my matlab version that's the problem?(R2010a)
Not sure how to fix this..
You need to call dir with an appropriate input string, that will choose the files that you need. Perhaps something like this:
S = dir('*.txt');
Also note that to use my FEX code natsortfiles you will need to download it (download button top right) and put the M-file on your MATLAB path (or in the current directory). You also need to do the same for the M-file of the other function that it needs to work: natsort.
Once you have both of those Mfiles, then try this example:
>> X = {'1.0.txt','1.5.txt','10.0.txt','10.5.txt','2.0.txt','2.5.txt'};
>> natsortfiles(X,'\d+\.?\d*')
ans =
'1.0.txt' '1.5.txt' '2.0.txt' '2.5.txt' '10.0.txt' '10.5.txt'
And if I would like to make an action between each file how can I handle it?
@Alikouider have a nested for loop. Inside get the names of the two files, then do whatever "action" you want to do. See the FAQ:

Sign in to comment.

More Answers (1)

You're going to have to get the base filename, and parse it to find the number. Try using sscanf() or str2double().

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 27 Jan 2016

Commented:

on 24 Jan 2022

Community Treasure Hunt

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

Start Hunting!