Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Sort mixed number/string data

Subject: Sort mixed number/string data

From: Alexis Riopel

Date: 3 Jul, 2012 19:53:06

Message: 1 of 3

Hi,

I have text data that looks like:

ZINC00390676 CAMBD_PM_Cent_modif -8.1 0.000 0.000 -3.62724 -2.09276 -18.7599
ZINC00390676 CAMBD_PM_Cent_modif -8.1 40.426 44.298 5.42284 7.22476 22.9496
ZINC00390676 CAMBD_PM_Cent_modif -7.8 6.553 8.975 -0.58864 0.78112 -24.4113
ZINC00390676 CAMBD_PM_Cent_modif -7.7 38.733 42.904 4.0946 8.61692 21.4168
ZINC00390676 CAMBD_PM_Cent_modif -7.6 38.855 41.792 -1.07264 14.5233 18.7207
ZINC00390676 CAMBD_PM_Cent_modif -7.6 1.824 8.358 -3.35408 -2.10728 -18.8821
ZINC00390676 CAMBD_PM_Cent_modif -7.6 4.476 6.208 -1.67296 -1.22144 -22.1985
ZINC00390676 CAMBD_PM_Cent_modif -7.5 35.885 40.756 -1.36496 14.0294 17.6917

I actually import the data with

result = textscan(fid, '%s%s%f%f%f%f%f%f');

wich gives me

result =

  Columns 1 through 5

    {540x1 cell} {540x1 cell} [540x1 double] [540x1 double] [540x1 double]

  Columns 6 through 8

    [540x1 double] [540x1 double] [540x1 double]

I can save each of the column in a specific variable. But what I want to do is to sort all my data according to column 3. What is the most simple way to do this?

Subject: Sort mixed number/string data

From: dpb

Date: 3 Jul, 2012 20:07:45

Message: 2 of 3

On 7/3/2012 2:53 PM, Alexis Riopel wrote:
> Hi,
>
> I have text data that looks like:
>
> ZINC00390676 CAMBD_PM_Cent_modif -8.1 0.000 0.000 -3.62724 -2.09276 -18.7599
> ZINC00390676 CAMBD_PM_Cent_modif -8.1 40.426 44.298 5.42284 7.22476 22.9496
 > ...
> I actually import the data with
> result = textscan(fid, '%s%s%f%f%f%f%f%f');
>
> wich gives me
>
> result =
> Columns 1 through 5
>
> {540x1 cell} {540x1 cell} [540x1 double] [540x1 double] [540x1 double]
>
> Columns 6 through 8
>
> [540x1 double] [540x1 double] [540x1 double]
>
> I can save each of the column in a specific variable. But what I want to
> do is to sort all my data according to column 3. What is the most simple
> way to do this?

Use the alternate return index variable in sort() and use it to reorder
the other cells according to the order of the sorted cell.

sortrows() could do all the numeric data in one swell foop but you would
still have to keep the index vector to rearrange the cell character data
in the corresponding row so there's probably little to gain in
converting portion to matrix. Although is case it might be a little
more handy if were to use the 'collectoutput' option in textscan and
have all the numeric data in a single cell array instead of the six.

--

--

Subject: Sort mixed number/string data

From: Jos (10584)

Date: 5 Jul, 2012 06:41:07

Message: 3 of 3

"Alexis Riopel" wrote in message <jsvij2$osm$1@newscl01ah.mathworks.com>...
> Hi,
>
> I have text data that looks like:
>
> ZINC00390676 CAMBD_PM_Cent_modif -8.1 0.000 0.000 -3.62724 -2.09276 -18.7599
> ZINC00390676 CAMBD_PM_Cent_modif -8.1 40.426 44.298 5.42284 7.22476 22.9496
> ZINC00390676 CAMBD_PM_Cent_modif -7.8 6.553 8.975 -0.58864 0.78112 -24.4113
> ZINC00390676 CAMBD_PM_Cent_modif -7.7 38.733 42.904 4.0946 8.61692 21.4168
> ZINC00390676 CAMBD_PM_Cent_modif -7.6 38.855 41.792 -1.07264 14.5233 18.7207
> ZINC00390676 CAMBD_PM_Cent_modif -7.6 1.824 8.358 -3.35408 -2.10728 -18.8821
> ZINC00390676 CAMBD_PM_Cent_modif -7.6 4.476 6.208 -1.67296 -1.22144 -22.1985
> ZINC00390676 CAMBD_PM_Cent_modif -7.5 35.885 40.756 -1.36496 14.0294 17.6917
>
> I actually import the data with
>
> result = textscan(fid, '%s%s%f%f%f%f%f%f');
>
> wich gives me
>
> result =
>
> Columns 1 through 5
>
> {540x1 cell} {540x1 cell} [540x1 double] [540x1 double] [540x1 double]
>
> Columns 6 through 8
>
> [540x1 double] [540x1 double] [540x1 double]
>
> I can save each of the column in a specific variable. But what I want to do is to sort all my data according to column 3. What is the most simple way to do this?

Your variable result is somewhat awkward being a 1-by- cell array. You would prefer to have it a 540-by-8 cell array with columns containing numbers and other columns containing character arrays.

If you can manage that, SORTROWS can handle cell arrays of mixed types perfectly well

A = {1 'b' 3'; 3 'a' 2 ; 2 'c' 1}
% [1] 'b' [3]
% [3] 'a' [2]
% [2] 'c' [1]

sortrows(A,2)
% [3] 'a' [2]
% [1] 'b' [3]
% [2] 'c' [1]

~ Jos
http://www.mathworks.nl/matlabcentral/fileexchange/authors/10584

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us