natsortfiles is not working for .png files
16 views (last 30 days)
Show older comments
I have downloaded the FEX from https://uk.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
To sort some of my png files in a folder. I have unzipped the natsortfiles folder and kept it in the current directory. While this is perfectly working for sorting the mat files in a folder, it is unrecognized when I am trying to use for image files (in png format).
I have used following code:
S = dir('raw*.png'); % get list of data files in directory that named with raw
S = natsortfiles(S); % sorts file names into natural order
N = length(S) ;
The error is "Unrecognized function or variable 'natsortfiles'."
Any suggestion/help, please?
0 Comments
Accepted Answer
Image Analyst
on 4 Jan 2022
Edited: Image Analyst
on 4 Jan 2022
S is a structure. First get the filenames, then call natsortfile:
S = dir('raw*.png'); % Get list of data files in directory that named with raw into a structure.
allFileNames = {S.name}; % Extract only the filenames from the structure.
numberOfFiles = length(allFileNames);
if numberOfFiles > 1
allFileNames = natsortfiles(allFileNames); % sorts file names into natural order
end
3 Comments
Image Analyst
on 5 Jan 2022
You said "I have unzipped the natsortfiles folder and kept it in the current directory." If natsortfiles() is in the current directory, you don't need to use addpath(), unless you unwisely used the cd command to switch to a different folder, which is virtually never a good idea. You should learn how to use fullfile() to construct the full file name, like
fullFileName = fullfile(S(k).folder, S(k).name);
See the FAQ:
It's nice to know that Stephen made natsortfiles() flexible enough to recognize and handle structures that are returned from dir().
Even better is to not create files with those kinds of names in the first place. Like if you have an integer k = 3, and might have 1000 files, make the filename with with a format specifier like 3.3d:
>> baseFileName = sprintf('%3.3d.png', 3)
baseFileName =
'003.png'
More Answers (1)
Stephen23
on 21 May 2023
Edited: Stephen23
on 21 May 2023
This page turns up relatively high on some Google searches, so I might as well make some corrections:
"S is a structure. First get the filenames, then call natsortfile:" is not required: NATSORTFILES is written to accept the structure returned by DIR directly. This is shown in both the Mfile help and the HTML documentation.
Nor is it really required to check the length and only call NATSORTFILES if there are multiple filenames: although internally NATSORTFILES does not have special case handling for zero or one filename**, it does handle them perfectly without any error.
So the simpler, recommended code (as shown in the NATSORTFILES help and documentation) is simply like this:
S = dir('raw*.png');
S = natsortfiles(S);
... which is exactly what the OP was using.
** I considered this but it a) just added complexity and b) gives inconsistent outputs, e.g. generating the 3rd output requires parsing the filenames anyway, at which point the actual sort is a only a minor additional runtime consumer.
3 Comments
Stephen23
on 21 May 2023
@Image Analyst: my guess is that the MAT files were in another directory, along with NATSORTFILES.
Clues are: lack of absolute/relative filename, so DIR would only work if the files were in the current directory. Then the statement "perfectly working for sorting the mat files in a folder" (bold added), which tells us that it worked in another folder, not the same as the current one where the PNG files are saved.
Image Analyst
on 21 May 2023
It wouldn't be the first time someone said they did it one way and actually did it a different way.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!