MATLAB Answers

0

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

Haksun Lee 님이 질문을 제출함. 18 Jun 2012
최근 활동 Stephen Cobeldick 님이 편집함. 21 Feb 2016
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

표시 이전 댓글 수: 2
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
Oh... probably... Meh, well, I'll stop being a grump and hack it out quickly again.
There was some misunderstanding..
Have a nice day everyone..
But I have to figure this out T.T
- Newbie programmer, H.S Lee

로그인 to comment.

답변 수: 1

Geoff 님의 답변 25 Jun 2012
 채택된 답변

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);

  댓글 수: 7

표시 이전 댓글 수: 4
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') );
@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'
@Walter thanks, it works now! @Stephan thank u for your answer

로그인 to comment.



Translated by