Sorting files with alphanumeric names in a structure array

Hi!
I'm trying to sort the text files of my struct array. The files are called:
Report (1).txt
Report (2).txt
.
.
.
Report (10).txt
Report (11).txt
.
.
.
Report (34).txt
What I want is something like:
Report (1).txt
Report (2).txt
Report (3).txt
Report (4).txt
.
.
.
Report (10).txt
But if I read directly without trying to sort them, this is the order in which they are read:
Report (1).txt
Report (10).txt
Report (11).txt
.
.
.
Report (2).txt
Report (20).txt
The amount of reports could be up to hundreds.
So, what I did to sort them is in the attached file.
However, for some reason it doesn't work properly and this is the output:
Report (1).TXT
Report (2).TXT
Report (2).TXT
Report (2).TXT
Report (2).TXT
Report (2).TXT
Report (2).TXT
Report (2).TXT
Report (2).TXT
Report (2).TXT
Report (2).TXT
Report (2).TXT
Report (3).TXT
Report (3).TXT
Report (3).TXT
Report (3).TXT
Report (3).TXT
Report (3).TXT
Report (3).TXT
Report (3).TXT
Report (3).TXT
Report (3).TXT
Report (3).TXT
Report (4).TXT
Report (4).TXT
Report (4).TXT
Report (4).TXT
Report (4).TXT
Report (4).TXT
Report (4).TXT
Report (4).TXT
Report (4).TXT
Report (4).TXT
Report (4).TXT
Report (5).TXT
Report (5).TXT
Report (5).TXT
Report (5).TXT
Report (5).TXT
Report (5).TXT
Report (5).TXT
Report (5).TXT
Report (5).TXT
Report (5).TXT
Report (5).TXT
Report (6).TXT
Report (6).TXT
Report (6).TXT
Report (6).TXT
Report (6).TXT
Report (6).TXT
Report (7).TXT
Report (8).TXT
Report (9).TXT
I'm getting crazy with this. I really don't find what I'm doing wrong. Could some of you please help me in this?

回答(1 个)

Look in the File Exchange; there are a couple of routines such as Natural Sort Filename, or Natural Sort

1 个评论

Thanks Walter. I saw these routines before but I'm more looking for an understanding of why mine is not doing the job rather than a quick solution to sort my files. It could be useful to solve future issues.

请先登录,再进行评论。

类别

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by