From: Jean-Philip Dumont on 23 Jul 2010 09:51 Hi, I would like to sort a cell array which contains names with letters and numbers. Here is a example : X = {'S1', 'H2', 'S3', 'H4', 'B6', .... 'H5'} When I use the fonction "sort", the sorting is made according to the letters in front of the numbers. This is not what I want to do. Is there a way to sort what I have according to the numbers beside the letters? For example, the result I would like to have is : X = {'S1', 'H2', 'S3', 'H4', 'H5', 'B6', .... } Thank you very much! JP
From: Jan Simon on 23 Jul 2010 10:23 Dear Jean-Philip, > X = {'S1', 'H2', 'S3', 'H4', 'B6', .... 'H5'} > sorting... > Xs = {'S1', 'H2', 'S3', 'H4', 'H5', 'B6', .... } You need to get the numbers at first: Worm = sprintf('%s,', X{:}); D = sscanf(Worm, '%c%d,'); Now the even indices are the numbers, which can be sorted: [dummy, Index] = sort(D(2:2:end)); And your original cell needs the same sorting index: Xs = X(Index); Another idea would be to let CELLFUN(@SSCANF) scan each cell string. If the initial string of each part has a varying number of characters, use STRREAD to parse the Worm. Good luck, Jan
From: Jean-Philip Dumont on 23 Jul 2010 10:46 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i2c8kc$epf$1(a)fred.mathworks.com>... > Dear Jean-Philip, > > > X = {'S1', 'H2', 'S3', 'H4', 'B6', .... 'H5'} > > sorting... > > Xs = {'S1', 'H2', 'S3', 'H4', 'H5', 'B6', .... } > > You need to get the numbers at first: > Worm = sprintf('%s,', X{:}); > D = sscanf(Worm, '%c%d,'); > Now the even indices are the numbers, which can be sorted: > [dummy, Index] = sort(D(2:2:end)); > And your original cell needs the same sorting index: > Xs = X(Index); > > Another idea would be to let CELLFUN(@SSCANF) scan each cell string. > If the initial string of each part has a varying number of characters, use STRREAD to parse the Worm. > > Good luck, Jan I used you first code and it does exactly what i need! Thank you very much for your help! JP
|
Pages: 1 Prev: Visual Studio 2008 PE with Matlab 7.0.4 Next: Get matrix position based on coordinates |