Hi everyone,
My code for sorting string. Question in comment:
s = ["1 9:53.3";
"3 9:23.5";
"5 2:16.2";
"2 2:45.6";
"4 12:01.2"]
s = split(s," ",2);
stab = array2table(s);
stab.s1 = double(stab.s1);
stab = sortrows(stab,"s1","ascend");
stab.s1 = string(stab.s1);
stab = table2array(stab)
% Question : How do I get the sorted matrix as a 5X1 matrix now?
Required sorted output matrix :
s =[ "1 9:53.334"
"2 2:45.623"
"3 9:23.545"
"4 12:01.213"
"5 2:16.245"]
Suggestions, if any, for better/more effficient ways to this string data are welcome.
Thanks for your time in advance.

댓글 수: 5

>> sortrows(s)
ans =
5×1 string array
"1 9:53.3"
"2 2:45.6"
"3 9:23.5"
"4 12:01.2"
"5 2:16.2"
>>
Why create such a monstrosity to begin with?
Why not read the data in as numeric and datetime and then create a formatted output if needed?
klb
klb 2021년 5월 5일
편집: klb 2021년 5월 5일
@dpb (edited: when it encounters 1,2,3,,,,11,12...20..sorts() orders data as 1,10,11...2,20,21..)
Any way around this?
Didnt think MATLAB can recognize numerical data within strings - it sorted fine upto 9 though.
"Why not read the data in as numeric and datetime and then create a formatted output if needed?" This is part of algo reading in a PDF. So everything is being read in as text.
"Didnt think MATLAB can recognize numerical data within strings. "
It doesn't. MATLAB sorts purely based on character code.
Try it with any "value" greater than 10, is that really the order you want?:
s = ["2 9:53.3";
"20 9:23.5";
"1 2:16.2";
"10 2:45.6";
"3 12:01.2"]
s = 5×1 string array
"2 9:53.3" "20 9:23.5" "1 2:16.2" "10 2:45.6" "3 12:01.2"
sort(s)
ans = 5×1 string array
"1 2:16.2" "10 2:45.6" "2 9:53.3" "20 9:23.5" "3 12:01.2"
klb
klb 2021년 5월 5일
편집: klb 2021년 5월 5일
Stephen, you got there before I did ! When it encounters 1,2,3,..11,12..20,21... it sorts data as 1,10,11...2,20,21...
dpb
dpb 2021년 5월 5일
편집: dpb 2021년 5월 5일
". it sorts data as 1,10,11...2,20,21... "
Of course it does; that was the point of asking why you're so stuck on trying to treat numeric data as character...note again that Stephen's solution has to convert to numeric first (as he also points to a better storage design) so why not just do that from the git-go instead of making it more difficult than needs be?
There is a lexical sort (and why TMW hasn't builtin one is anybody's guess) on FEX altho I don't have the link right now at hand...

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2021년 5월 5일
편집: Stephen23 2021년 5월 5일

1 개 추천

s = ["1 9:53.3";
"3 9:23.5";
"5 2:16.2";
"2 2:45.6";
"4 12:01.2"];
[~,X] = sort(str2double(extractBefore(s,' ')));
out = s(X,:)
out = 5×1 string array
"1 9:53.3" "2 2:45.6" "3 9:23.5" "4 12:01.2" "5 2:16.2"
Note that much better data design would store numeric data as a numeric type.

댓글 수: 6

klb
klb 2021년 5월 5일
편집: klb 2021년 5월 5일
This is part of algo reading in a PDF. So everything is being imported first as text.
klb
klb 2021년 5월 5일
Stephen, encountered double digit numbers and your solution works. Thank you!
dpb
dpb 2021년 5월 5일
"reading in a PDF. So everything is being imported first as text."
Still would be far easier going forward to first convert that text to the proper type instead of trying to fit square peg in round hole...
klb
klb 2021년 5월 6일
Please demostrate. You can start with that scrambled matrix
dpb
dpb 2021년 5월 6일
편집: dpb 2021년 5월 6일
>> s=split(s);
>> data=table(str2double(s(:,1)), ...
duration(s(:,2),'InputFormat','mm:ss.S','Format','mm:ss.S'), ...
'VariableNames',{'N','T'})
data =
5×2 table
N T
_ _______
1 09:53.3
3 09:23.5
5 02:16.2
2 02:45.6
4 12:01.2
>> sortrows(data,'N')
ans =
5×2 table
N T
_ _______
1 09:53.3
2 02:45.6
3 09:23.5
4 12:01.2
5 02:16.2
>>
Or, alternatively, if were interested in the time/duration
>> sortrows(data,'T')
ans =
5×2 table
N T
_ _______
5 02:16.2
2 02:45.6
3 09:23.5
1 09:53.3
4 12:01.2
>>
Nothing could be easier; NB: the table is a wonderfully powerful container for mixed data types; much more flexible than just a cell array and far more convienent than holding in either separate arrays or to try to machinate on text when it really isn't text...
If you were to have begun with a text file instead of a string array, you surely would have used readtable or similar tool to import as the type of data in the file; the message is to treat the source text the same way despite its origin.
klb
klb 2021년 5월 6일
Thanks for the time and feedback @dbp Follwing the answer to this question I already went ahead with remainder of the data processing; it includes steps same as first option you share. Well, after sorting. Your solutions are noted. Please post it as an answer poper - It will have better visibilty to the community and I can upvote it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chunru
Chunru 2021년 5월 5일

0 개 추천

try the following:
s = stab(:,1) + " " + stab(:,2)
Not sure how can you get the additional digits not in the input.

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

질문:

klb
2021년 5월 4일

편집:

dpb
2021년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by