Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7408101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:51:05+00:00 2026-05-29T05:51:05+00:00

I have a set of data and ask Matlab to sort in ascending order

  • 0

I have a set of data and ask Matlab to sort in ascending order like this

filename=input('Type filename.txt: ','s');
fid=fopen(filename);
mydata=textscan(fid,'%f %c','headerlines', 1, 'delimiter','\t');
fclose(fid);

% sort data
[mydata{1},idx] = sort(mydata{1})
mydata{2} =  mydata{2}(idx)

Data came out like this:

0.41    U
0.41    U
0.41    U
0.41    D
0.41    U
0.41    D
0.41    U
0.41    U
0.41    D
0.41    U
0.42    U
0.42    D
0.42    U
0.42    U
0.42    U
0.42    D
0.43    U

U = undetect and D = Detects. If you notice carefully, if the first column has the same number, say 0.41, Matlab would sort the first column and disregard the order of the second column. What I would like Matlab to do is if it sees the same number in the first column, then place the number based on the second column, U first before D.
The final sorted data would like this:

0.41    U
0.41    U
0.41    U
0.41    U
0.41    U
0.41    U
0.41    U
0.41    D
0.41    D
0.41    D
0.42    U
0.42    U
0.42    U
0.42    U
0.42    D
0.42    D
0.43    U

If somehow it would make it easier, I can make 1=Detect and 0=Nondetect
Thank you for your help and time.

Edit: I should also add that sortrow does not work because I would like to preserve the data structure so that the following code would follow

o=mydata{1} %index the first column of the array mydata


c = zeros(size(mydata,1),1); % preallocate empty matrix 
c = mydata{2} == 'U';
for i = 1:size(mydata,1)
 curValue = mydata{i,2};
 data{i,3} =  ~isempty(curValue) && ischar(curValue) && strcmp(curValue ,'U');

end

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:51:06+00:00Added an answer on May 29, 2026 at 5:51 am

    You can use sortrows, but you have to convert mydata into a n-by-2 cell array first:

    mydata = [num2cell(mydata{1}),num2cell(mydata{2})];
    
    cs = sortrows(mydata,[1,-2]) %# sort the first col ascending, then second descending
    cs = 
        [0.41]    'U'
        [0.41]    'U'
        [0.41]    'U'
        [0.41]    'U'
        [0.41]    'U'
        [0.41]    'U'
        [0.41]    'U'
        [0.41]    'D'
        [0.41]    'D'
        [0.41]    'D'
        [0.42]    'U'
        [0.42]    'U'
        [0.42]    'U'
        [0.42]    'U'
        [0.42]    'D'
        [0.42]    'D'
        [0.43]    'U'
    

    You can backtransform it by calling

    mydataSorted = {cat(1,cs{:,1}),cat(1,cs{:,2})};
    

    Obviously, if you transform your data so that mydata{2} is numeric, you can simply forego the cell array and call sortrows straight away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a set of data, with columns x and y. This set contains
i have a .csv data set (like 15000 items) and i want to find
I have set the data to div element and append it to another div
I have a set of data, and while the number of fields and tables
I have a set of data points in 3D space which apparently all fall
I have a set of data and I want to find the biggest and
I have a set of data that contains garbled text fields because of encoding
I have a set of data in one JSON structure: [[task1, 10, 99], [task2,
I have a set of data with over 1000 entries. I can filter these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.