natsortfiles is not working for .png files

16 ビュー (過去 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 Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by