Sort characters w/ number in a field of struct array

Capture.PNG
Hi everyone,
I am wondering what function should I use to reorgnize the name in a certain way (e.g., descending). I think "sort" function does not work here.
Thank you.

3 Comments

Hi, It seems like natsortfiles function does not work..
This one helped!
[~, reindex] = sort( str2double( regexp( {A.name}, '\d+', 'match', 'once' )))
A = A(reindex) ;
What does "reorganize the names in a certain way" exactly mean? Which way? I guess per isakson's idea hits the point, but it is easier, if you reveal, what you want to achieve.
I wanted to sort the field of "name" in the descending order, starting from ~_1.scv to ~_end number.scv. I agree with what you said!

Sign in to comment.

More Answers (1)

You could download natsortfiles, which now directly sorts the DIR output structure:
S = dir(..);
S = natsortfiles(S); % alphanumeric sort by filename

1 Comment

As a beginner of MATLAB who is avidly learning lots of features, I am realizing the importance of reading through documentations. Thank you for your tip!

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Asked:

on 29 Dec 2018

Edited:

on 18 Apr 2021

Community Treasure Hunt

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

Start Hunting!