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

조회 수: 6 (최근 30일)
Haksun Lee
Haksun Lee 2012년 6월 18일
댓글: Stephen23 2024년 2월 8일
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일
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일
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
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개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by