I am surprised that my FEX submission was no use to you, considering that it was written to sort filenames alphanumerically, exactly as you seem to wish to do. Not only that, it was extensively texted, and I have not had one single person report that it does not work as documented, so perhaps:
- you have discovered a bug that no one else found: please let me know exactly how to replicate this bug and I will fix it.
- you did not download it form FEX: please download it from the given link, unzip it, and put the files onto your MATLAB path.
- you want a different order (not a "natural order"): please give an example of the order that you require.
- you are using it incorrectly: please note that there are usage examples in the FEX description, usage examples in the HTML help, and usage examples in the Mfile help. Anyone with internet access can read all of those examples for free. Did you look at them? Did you try them?
In case it your favorite internet search engine is momentarily not working and you do not wish to try using another internet search engine to easily locate those examples yourself, I have copied one of them here:
P = 'absolute/relative path to where the files are svaed';
S = dir(fullfile(P,'*.txt')); % get list of files in directory
S = natsortfiles(S); % sort file names into order
for k = 1:numel(S)
F = fullfile(P,S(k).name)
end
Which is trivially adapted to your code like this:
baseFileNames = dir(filePattern);
baseFileNames = natsortfiles(baseFileNames);
imageArray = cell(size(baseFileNames));
for k = 1:numel(baseFileNames)
fullFileName = fullfile(thisFolder, baseFileNames(k).name);
fprintf(' Processing image file %s\n', fullFileName);
imageArray{f} = imread(fullFileName);
...
end
I also fixed several other "features" in your code, such as correctly preallocating the cell array before the loop, and removing the totally pointless if.