Sorting complex strings by a deliminator

1 view (last 30 days)
Diana Hardy
Diana Hardy on 21 Aug 2020
Commented: Diana Hardy on 15 Sep 2020
Hello!
I am currenty sorting a cell array by the first column, which is made up of ID codes stored as strings. The ID's are made up of numbers and characters, separated by dashes. Here is what it looks like (only pay attention to the first column, the rest isn't the actual data):
'c1-3' 247 247 247 97 0 0
'c1-4' 247 247 247 97 0 0
'c1-5' 247 247 247 97 0 0
'c1-1' 247 247 247 97 0 0
'c1-2' 247 247 247 97 0 0
'c2-11' 195 195 195 78 0 0
'c2-4' 195 195 195 78 0 0
'c2-7' 195 195 195 78 0 0
'c2-2' 195 195 195 78 0 0
'c2-9' 195 195 195 78 0 0
'c2-8' 195 195 195 78 0 0
'c2-10' 195 195 195 78 0 0
'c2-6' 195 195 195 78 0 0
'c2-3' 195 195 195 78 0 0
'c2-5' 195 195 195 78 0 0
'c2-1' 195 195 195 78 0 0
'c3-5' 127 127 127 53 5 0
'c3-7' 127 127 127 53 5 0
'c3-2' 127 127 127 53 5 0
'c3-1' 127 127 127 53 5 0
'c3-9' 127 127 127 53 5 0
'c3-6' 127 127 127 53 5 0
'c3-8' 127 127 127 53 5 0
'c3-4' 127 127 127 53 5 0
'c3-3' 127 127 127 53 5 0
This is just my sample code, realistically I would have ID's with more values like '1-4-3-A'
I want it to each section that is separated by a dash and sort by the total value if it's a number, rather than one digit at a time. Unfortunately, the way the regular sortrows function works gives me issues when I have double digit numbers. For example, in this array, I have an issue where c2-10 and c2-11 come before c2-2 so rather than getting this:
'c1-1' 247 247 247
'c1-2' 247 247 247
'c1-3' 247 247 247
'c1-4' 247 247 247
'c1-5' 247 247 247
'c2-1' 195 195 195
'c2-2' 195 195 195
'c2-3' 195 195 195
'c2-4' 195 195 195
'c2-5' 195 195 195
'c2-6' 195 195 195
'c2-7' 195 195 195
'c2-8' 195 195 195
'c2-9' 195 195 195
'c2-10' 195 195 195
'c2-11' 195 195 195
'c3-1' 127 127 127
'c3-2' 127 127 127
'c3-3' 127 127 127
'c3-4' 127 127 127
'c3-5' 127 127 127
'c3-6' 127 127 127
'c3-7' 127 127 127
'c3-8' 127 127 127
'c3-9' 127 127 127
I get this:
'c1-1' 247 247 247
'c1-2' 247 247 247
'c1-3' 247 247 247
'c1-4' 247 247 247
'c1-5' 247 247 247
'c2-1' 195 195 195
'c2-10' 195 195 195
'c2-11' 195 195 195
'c2-2' 195 195 195
'c2-3' 195 195 195
'c2-4' 195 195 195
'c2-5' 195 195 195
'c2-6' 195 195 195
'c2-7' 195 195 195
'c2-8' 195 195 195
'c2-9' 195 195 195
'c3-1' 127 127 127
'c3-2' 127 127 127
'c3-3' 127 127 127
'c3-4' 127 127 127
'c3-5' 127 127 127
'c3-6' 127 127 127
'c3-7' 127 127 127
'c3-8' 127 127 127
'c3-9' 127 127 127
Does anyone know if a fuction exists within matlab that would sort it the way I'm looking for?

Answers (1)

Bruno Luong
Bruno Luong on 21 Aug 2020
One way
c = {'c1-3' 247 247 247 97 0 0
'c1-4' 247 247 247 97 0 0
'c1-5' 247 247 247 97 0 0
'c1-1' 247 247 247 97 0 0
'c1-2' 247 247 247 97 0 0
'c2-11' 195 195 195 78 0 0
'c2-4' 195 195 195 78 0 0
'c2-7' 195 195 195 78 0 0
'c2-2' 195 195 195 78 0 0
'c2-9' 195 195 195 78 0 0
'c2-8' 195 195 195 78 0 0
'c2-10' 195 195 195 78 0 0
'c2-6' 195 195 195 78 0 0
'c2-3' 195 195 195 78 0 0
'c2-5' 195 195 195 78 0 0
'c2-1' 195 195 195 78 0 0
'c3-5' 127 127 127 53 5 0
'c3-7' 127 127 127 53 5 0
'c3-2' 127 127 127 53 5 0
'c3-1' 127 127 127 53 5 0
'c3-9' 127 127 127 53 5 0
'c3-6' 127 127 127 53 5 0
'c3-8' 127 127 127 53 5 0
'c3-4' 127 127 127 53 5 0
'c3-3' 127 127 127 53 5 0 }
x=cellfun(@(s) textscan(s,'c%d-%d'), c(:,1),'unif', 0);
x=[x{:}];
x=reshape([x{:}],2,[])';
[~,is]=sortrows(x);
c(is,:)
  1 Comment
Diana Hardy
Diana Hardy on 15 Sep 2020
is there a way to do this with a more general format of ID's? not all of my ID's I'll use start with a c, but they all have a few values separated by dashes. For example, another set of values I might use this app for would look like what I show below, and I would want the 1-20 group at the end rather than right after the 1-2 group
c = {'1-2-1-a' 247 247 247 97 0 0
'1-2-2-a' 247 247 247 97 0 0
'1-2-3-a' 247 247 247 97 0 0
'1-3-1-a' 247 247 247 97 0 0
'1-3-2-a' 247 247 247 97 0 0
'1-3-3-a' 195 195 195 78 0 0
'1-3-1-a' 195 195 195 78 0 0
'1-4-2-a' 195 195 195 78 0 0
'1-4-3-a' 195 195 195 78 0 0
'1-4-1-a' 195 195 195 78 0 0
'1-5-2-a' 195 195 195 78 0 0
'1-5-3-a' 195 195 195 78 0 0
'1-5-2-a' 195 195 195 78 0 0
'1-6-1-a' 195 195 195 78 0 0
'1-6-2-a' 195 195 195 78 0 0
'1-6-3-a' 195 195 195 78 0 0
... (more rows following this pattern)
'1-20-1-a' 247 247 247 97 0 0
'1-20-2-a' 247 247 247 97 0 0
'1-20-3-a' 247 247 247 97 0 0

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!