natsortfiles is not working for .png files

조회 수: 7 (최근 30일)
Anu
Anu 2022년 1월 4일
댓글: Image Analyst 2023년 5월 21일
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?

채택된 답변

Image Analyst
Image Analyst 2022년 1월 4일
편집: Image Analyst 2022년 1월 4일
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
Image Analyst
Image Analyst 2022년 1월 5일
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'
Anu
Anu 2022년 1월 5일
Thanks for your great suggestion. I am very new to Matlab but this forum is actually helping me to learn a lot of things. I completely agree with you, it's great to have natsortfiles() that Stephen had created. Thanks once again.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2023년 5월 21일
편집: Stephen23 2023년 5월 21일
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
Stephen23
Stephen23 2023년 5월 21일
@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
Image Analyst 2023년 5월 21일
It wouldn't be the first time someone said they did it one way and actually did it a different way.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by