フィルターのクリア

file list when "dir" to import data

2 ビュー (過去 30 日間)
Sunghwan Kim
Sunghwan Kim 2022 年 6 月 29 日
コメント済み: Image Analyst 2022 年 6 月 30 日
I am using "dir" to import .txt filedata. Here is the simple code that I used.
liste = dir('*.txt');
My text files has number in the name. The problem is that dir read the file that has "10" ahead of "2". The file names in the "liste" are listed as follows.
How can I make the "liste" to have the sequentially increasing number in the name?
*1.txt, *2.txt,*3.txt,...
Thanks in advance.

採用された回答

Stephen23
Stephen23 2022 年 6 月 29 日
"How can I make the "liste" to have the sequentially increasing number in the name?"
If you cannot change the filenames to use leading zeros then you will need to sort the filenames yourself. The easy way to do that is to download the function NATSORTFILES
and use it like this:
liste = natsortfiles(liste);
  2 件のコメント
Sunghwan Kim
Sunghwan Kim 2022 年 6 月 30 日
Great Thanks.
Image Analyst
Image Analyst 2022 年 6 月 30 日
What about both our suggestions to change the way the filenames are created. Would that be a possibility? If so, then you don't need this third-party natsortfiles() function. (I'm guessing not otherwise you would have, and that you got the files already named that from some other person or place.)

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 6 月 29 日
It's best if you can just add leading zeros when you create the files, like
baseFileName = sprintf('blah blah blah fubar-ROI%3.3d.TXT', yourNumber);
The %3.3d will make sure numbers appear as 001, 002, .... 010, 011, ......200, ..... 999 etc.
If your max number is bigger than 3 digits then use 4.4 or 5.5 or whatever.
Otherwise see

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by