How can I make imageDatastore sort naturally?

7 ビュー (過去 30 日間)
Pranay Koppula
Pranay Koppula 2021 年 10 月 8 日
編集済み: Kamal 2022 年 12 月 28 日
When I use imageDatastore to store files, the files are sorted according to their names like so:
This is how MATLAB sorts them by default. How can I make it store the files in an image datastore in the correct order ('1,5,10,15,20' rather than '1,10,15,20,5')? Or is there a way to create a new datastore with the sorted filenames? I understand that sort_nat or natsortfiles could be used in some way, but I'm not sure how. I can't sort and store the images in any other way(like in a table or directory), it has to be an image datastore.

採用された回答

Pranay Koppula
Pranay Koppula 2021 年 10 月 8 日
I was able to resolve this finally by doing this:
imds=imageDatastore(filepathofimages);
imds.Files=sort_nat(imds.Files);
Luckily, I only needed to sort the filenames for my project. I don't know how this affects other attributes of the datastore, and to anyone else who might consider trying this solution, I would recommend experimenting with a test datastore to see how this affects other attributes.
  2 件のコメント
Ameetha
Ameetha 2022 年 8 月 30 日
But Where is sort_nat ? unrecognised function
Kamal
Kamal 2022 年 12 月 28 日
編集済み: Kamal 2022 年 12 月 28 日
For some reason i don't know, when you assign the sorted files to imds object which is imds.Files, it removes all the labels that were already assigned by the imagedatastore function which is really important for me later.
Any idea or workaround to avoid losing the labels assigned earlier? @Pranay Koppula @Stephen23

サインインしてコメントする。

その他の回答 (1 件)

yanqi liu
yanqi liu 2021 年 10 月 8 日
sir,may be use the 001、002、…… as image filename
  3 件のコメント
Stephen23
Stephen23 2021 年 10 月 8 日
@Pranay Koppula: actually, if you use sufficient leading zeros then sorting by character does resolve this problem, exactly as yanqi liu writes. This is a robust and very efficient solution.
Pranay Koppula
Pranay Koppula 2021 年 10 月 9 日
You're right, I realised this was true. However, when the total number of frames is not known, we can't know how many leading zeroes we need. In most cases though, it is indeed a robust solution.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by