Converting to numerical ordering from ASCII ordering

3 views (last 30 days)
Hi All,
I am stuck trying to get Matlab to process my files in numerical order. When I use dir() and it processes each image in there during a for loop, it processes them according to ascii ordering. How can I get my code to process them in numerical order?
I tried using thr function 'natsort' but apparently on matlab R2015b it doesn't exist!
Here is what my processed images are looking like. Notice the order (1,10,11,12 etc...)
Now reading H:\Documents\o2\Processed Image1.jpg0 The experiment is not complete yet. The number of Black Pixels is: 27
Now reading H:\Documents\o2\Processed Image10.jpg1 The experiment is not complete yet. The number of Black Pixels is: 2
Now reading H:\Documents\o2\Processed Image11.jpg1 The experiment is not complete yet. The number of Black Pixels is: 12
Now reading H:\Documents\o2\Processed Image12.jpg1 The experiment is not complete yet. The number of Black Pixels is: 13
Now reading H:\Documents\o2\Processed Image13.jpg1 The experiment is not complete yet. The number of Black Pixels is: 24
Now reading H:\Documents\o2\Processed Image14.jpg1 The experiment is not complete yet. The number of Black Pixels is: 117
Now reading H:\Documents\o2\Processed Image15.jpg1 The experiment is not complete yet. The number of Black Pixels is: 18
Now reading H:\Documents\o2\Processed Image16.jpg1 The experiment is not complete yet. The number of Black Pixels is: 10
Now reading H:\Documents\o2\Processed Image17.jpg1 The experiment is not complete yet. The number of Black Pixels is: 159
Now reading H:\Documents\o2\Processed Image18.jpg1 The experiment is not complete yet. The number of Black Pixels is: 159
Now reading H:\Documents\o2\Processed Image19.jpg1 The experiment is not complete yet. The number of Black Pixels is: 41
Now reading H:\Documents\o2\Processed Image2.jpg0 The experiment is not complete yet. The number of Black Pixels is: 0
Now reading H:\Documents\o2\Processed Image20.jpg1 The experiment is not complete yet. The number of Black Pixels is: 229
Now reading H:\Documents\o2\Processed Image21.jpg1 The experiment is not complete yet. The number of Black Pixels is: 194
Now reading H:\Documents\o2\Processed Image22.jpg1 The experiment is not complete yet. The number of Black Pixels is: 110
Now reading H:\Documents\o2\Processed Image23.jpg1 The experiment is not complete yet. The number of Black Pixels is: 225
Now reading H:\Documents\o2\Processed Image24.jpg1 The experiment is not complete yet. The number of Black Pixels is: 375
Now reading H:\Documents\o2\Processed Image25.jpg1 The experiment is not complete yet. The number of Black Pixels is: 125
Now reading H:\Documents\o2\Processed Image26.jpg1 The experiment is not complete yet. The number of Black Pixels is: 869
Now reading H:\Documents\o2\Processed Image27.jpg1 The experiment is not complete yet. The number of Black Pixels is: 568
Now reading H:\Documents\o2\Processed Image28.jpg1 The experiment is not complete yet. The number of Black Pixels is: 547
Now reading H:\Documents\o2\Processed Image29.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1121
Now reading H:\Documents\o2\Processed Image3.jpg0 The experiment is not complete yet. The number of Black Pixels is: 5
Now reading H:\Documents\o2\Processed Image30.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1875
Now reading H:\Documents\o2\Processed Image31.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1617
Now reading H:\Documents\o2\Processed Image32.jpg1 The experiment is not complete yet. The number of Black Pixels is: 2108
Now reading H:\Documents\o2\Processed Image33.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1849
Now reading H:\Documents\o2\Processed Image4.jpg0 The experiment is not complete yet. The number of Black Pixels is: 21
Now reading H:\Documents\o2\Processed Image5.jpg0 The experiment is not complete yet. The number of Black Pixels is: 5
Now reading H:\Documents\o2\Processed Image6.jpg0 The experiment is not complete yet. The number of Black Pixels is: 2
Now reading H:\Documents\o2\Processed Image7.jpg0 The experiment is not complete yet. The number of Black Pixels is: 2
Now reading H:\Documents\o2\Processed Image8.jpg0 The experiment is not complete yet. The number of Black Pixels is: 1
Now reading H:\Documents\o2\Processed Image9.jpg0 The experiment is not complete yet. The number of Black Pixels is: 5
any ideas?
many thanks,
Ellis

Answers (1)

Stephen23
Stephen23 on 15 Jun 2016
Edited: Stephen23 on 18 Apr 2021
The order returned by dir is not specified by its documentation. This means you need to sort its output data yourself.
There is not inbuilt function to do what you want, but you can use my FEX submission natsortfiles, which I wrote to solve this exact problem of sorting file names, taking into account any numbers in their names:
You can use it something like this:
S = dir('*.jpg');
S = natsortfiles(S);
for k = 1:numel(A)
file = S(k).name;
... your code
end

Categories

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!