What do I have to do to sort strings properly in Listbox?

Hi,
While I try to figure this out, I have problems again..
To sort strings in listbox,
e.g.,I have strings below! 'p1' 'p2' 'p10' 'p12' 'p21'
To sort those strings, I tried to use sort(),
and then, result was like this..
'p1' 'p10' 'p12' 'p2' 'p21'
%%%% :-( %%%%
But, I want to sort A as, 'p1' 'p2' 'p10' 'p12' 'p21'
What do I have to do for this?
I'm looking forward to your answer!

5 件のコメント

Geoff
Geoff 2012 年 6 月 25 日
Hey Haksun Lee... I answered your question with code, and then you deleted the question. If you think I'll answer it again, you're mistaken.
Walter Roberson
Walter Roberson 2012 年 6 月 25 日
Geoff, if you answered the copy of the question that was posted about half an hour ago, then I'm the one who deleted that question. There were no responses at the time I looked, but it could be that an answer of yours came in between the time I loaded the question and the time I hit the delete.
Haksun Lee
Haksun Lee 2012 年 6 月 25 日
what was happened?-0-
Because there were no answers since I asked about sorting, so I re-ask same question today.
Maybe Geoff answered it, BUT I couldn't check your answers...sorry
Geoff
Geoff 2012 年 6 月 25 日
Oh... probably... Meh, well, I'll stop being a grump and hack it out quickly again.
Haksun Lee
Haksun Lee 2012 年 6 月 25 日
There was some misunderstanding..
Have a nice day everyone..
But I have to figure this out T.T
- Newbie programmer, H.S Lee

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

 採用された回答

Geoff
Geoff 2012 年 6 月 25 日

0 投票

Don't ask the same question just because nobody answered it yesterday. If you are impatient, write a comment on it to bump it back up. People occasionally check through old questions to see what hasn't been answered. That's also a reason to accept answers - so people who do look back on old questions know not to check those that are already accepted.
Now... code, with no explanation this time.
A = {'p1', 'p10', 'p12', 'p2', 'p21'};
N = cellfun( @str2double, regexp(A, '\d+', 'match') )
[~,I] = sort(N);
B = A(I);

8 件のコメント

Haksun Lee
Haksun Lee 2012 年 6 月 25 日
Thank you Geoff
I've learned lots of things this time!
I will keep them in my mind.
Thank you so much again.
Walter Roberson
Walter Roberson 2012 年 6 月 25 日
編集済み: Walter Roberson 2016 年 2 月 20 日
This can be made shorter:
N = str2double( regexp(A, '\d+', 'match') );
That is, str2double() is able to work on cell arrays of strings, and returns a numeric matrix when it does so.
Geoff
Geoff 2012 年 6 月 25 日
@Walter : try it... N = [NaN, NaN, NaN, NaN, NaN]
Soroush Asarzadeh
Soroush Asarzadeh 2016 年 2 月 20 日
Hello Geoff, I have a similar question, but in my case: A = {'p10=34', 'p4=10', 'p2=11', 'p3=90', 'p21=55'}; so the next line N = cellfun( @str2double, regexp(A, '\d+', 'match') ) causes an error because of the '=' between figures. Do you know how can i fix it? Thanks
Walter Roberson
Walter Roberson 2016 年 2 月 20 日
The problem is not the "=" in itself, the problem is that you have not defined which sequence of digits you want to extract out of the two that occur in each string. If you want to extract the first set, then use
N = cellfun( @str2double, regexp(A, '\d+', 'match', 'once') );
Stephen23
Stephen23 2016 年 2 月 20 日
編集済み: Stephen23 2016 年 2 月 21 日
@Soroush Asarzadeh: This is trivial using my FEX submission natsort:
>> A = {'p10=34', 'p4=10', 'p2=11', 'p3=90', 'p21=55'};
>> natsort(A)
ans =
'p2=11' 'p3=90' 'p4=10' 'p10=34' 'p21=55'
Soroush Asarzadeh
Soroush Asarzadeh 2016 年 2 月 20 日
@Walter thanks, it works now! @Stephan thank u for your answer
Stephen23
Stephen23 2024 年 2 月 8 日
Note that Walter Roberson's approach from 25 Jun 2012 can be made to work with the addition of the 'once' option (and this is indeed more efficient than calling STR2DOUBLE inside CELLFUN):
A = {'p10=34', 'p4=10', 'p2=11', 'p3=90', 'p21=55'};
N = str2double(regexp(A, '\d+', 'match', 'once'))
N = 1x5
10 4 2 3 21

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLanguage Support についてさらに検索

質問済み:

2012 年 6 月 18 日

コメント済み:

2024 年 2 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by