Thread Subject: Sort rows function

Subject: Sort rows function

From: Simon Luk

Date: 22 Mar, 2009 10:44:01

Message: 1 of 6

Hi All,

I would like to apply the sortrows function to the following fuction

meh =

    'barley' [1]
    'corn' [3]
    'earn' [1]
    'grain' [3]
    'meal-feed' [1]
    'oat' [2]
    'oilseed' [2]
    'rye' [1]
    'sorghum' [2]
    'soy-meal' [1]
    'soybean' [2]
    'sugar' [2]
    'veg-oil' [1]
    'wheat' [2]


But unfortunately it gave me this error

sortrows(meh,2)
??? Error using ==> char
Cell elements must be character arrays.

Error in ==> sortrows>sort_cell_back_to_front at 129
    tmp = char(x(ndx,k));

Error in ==> sortrows at 90
        ndx = sort_cell_back_to_front(x_sub, col);


HOw would I fix this issue?

Thanks in advance



Regards,
Simon

Subject: Sort rows function

From: Bruno Luong

Date: 22 Mar, 2009 11:47:01

Message: 2 of 6

"Simon Luk" <simon.luk@student.uts.edu.au.REMOVETHIS> wrote in message <gq54ph$po5$1@fred.mathworks.com>...
> Hi All,
>
> I would like to apply the sortrows function to the following fuction
>
> meh =
>
> 'barley' [1]
> 'corn' [3]
> 'earn' [1]
> 'grain' [3]
> 'meal-feed' [1]
> 'oat' [2]
> 'oilseed' [2]
> 'rye' [1]
> 'sorghum' [2]
> 'soy-meal' [1]
> 'soybean' [2]
> 'sugar' [2]
> 'veg-oil' [1]
> 'wheat' [2]
>
>
> But unfortunately it gave me this error
>
> sortrows(meh,2)
> ??? Error using ==> char
> Cell elements must be character arrays.
>
> Error in ==> sortrows>sort_cell_back_to_front at 129
> tmp = char(x(ndx,k));
>
> Error in ==> sortrows at 90
> ndx = sort_cell_back_to_front(x_sub, col);
>
>
> HOw would I fix this issue?
>

You have mix chararcter and numerical in your cellarray. sortrows in older Matlab version requires all are character arrays (the error message is very useful sometime).

I assume you want to sort by with respect to the first column of your cell (?). In this case please do this:

c={'a' 1;
   'ab' 2;
   'aaa' 3}

[dummy is]=sortrows(c(:,1))
c=c(is,:)

% Alternatively you can convert all of elements in string before sorting:

c={'a' 1;
   'ab' 2;
   'aaa' 3}

cstr = cellfun(@num2str,c,'uni',false)
sortrows(cellfun(@num2str,cstr,'uni',false))

% Bruno

Subject: Sort rows function

From: Simon Luk

Date: 22 Mar, 2009 11:53:01

Message: 3 of 6

Hi Bruno,

I should of said this earlier, I want to sort with respect to 2nd column decending order.
So how would I do that?

Subject: Sort rows function

From: Bruno Luong

Date: 22 Mar, 2009 12:04:01

Message: 4 of 6

"Simon Luk" <simon.luk@student.uts.edu.au.REMOVETHIS> wrote in message <gq58qt$dtq$1@fred.mathworks.com>...
> Hi Bruno,
>
> I should of said this earlier, I want to sort with respect to 2nd column decending order.
> So how would I do that?

c={'a' 1;
   'ab' 2;
   'aaa' 3}

[dumm is]=sort([c{:,2}],'descend')
c=c(is,:)

% Bruno

Subject: Sort rows function

From: Simon Luk

Date: 22 Mar, 2009 12:10:03

Message: 5 of 6

thanks bruno... i surely can sleep well tonight.

Subject: Sort rows function

From: Bill Kortebein

Date: 6 Apr, 2009 18:57:01

Message: 6 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gq59fh$nvg$1@fred.mathworks.com>...
> "Simon Luk" <simon.luk@student.uts.edu.au.REMOVETHIS> wrote in message <gq58qt$dtq$1@fred.mathworks.com>...
> > Hi Bruno,
> >
> > I should of said this earlier, I want to sort with respect to 2nd column decending order.
> > So how would I do that?
>
> c={'a' 1;
> 'ab' 2;
> 'aaa' 3}
>
> [dumm is]=sort([c{:,2}],'descend')
> c=c(is,:)
>
> % Bruno

Bruno, this also answers a question that I really needed to know the answer to as well, so I appreciate it.
After studying your commands and examining Matlab help, I finally understand what you did.
But wow... as a long-time Excel user and a relatively new Matlab user, to me it makes no sense that Mathworks would require nearly impenatrable statements like these to do a simple sort. Um -- just let me provide the column I want to sort on, and sort away! Of course, "sortrows" is like that, but it seems about impossible to make it work on matrices with mixed text and numeric data. Oh, well.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
sortrows Simon Luk 22 Mar, 2009 06:47:36
rssFeed for this Thread

Contact us at files@mathworks.com