How to split alphanumerical string without delimiter?

Dear community,
I am dealing with .csv tables which contain one column of strings 'A1';'A2';...;'A100';...;'An';'B1';'B2';...;'B100';...;'Bn';'C1';'C2';...;'C100';...;'Cn';. I need to have them sorted to 'A1';'B1';'C1';'A2';'B2';'C2';...;'A100';'B100';'C100';...;'An';'Bn';'Cn'. After trying sort_nat, natsort, sortrows I tried splitting the strings which I do not know how to do without delimititer like e.g. a semicolon and sort for the numerical part. So, question: how to split strings by character without delimiter and sort by the numerical part? Thank you in adavance.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 10월 14일
f = fopen('Data20161014.csv'); % Data20161014.csv - your csv file
c = textscan(f,'%s','delimiter','\n');
fclose(f);
[~,ii] = sort(str2double(regexp(c{:},'\d+','match','once')));
out = c{:}(ii);

댓글 수: 5

Mario
Mario 2016년 10월 14일
Thank you very much Andrei Bobrov for this fast response! Of course it works. Where, however, do I tell the target colum to sort by, which is the sixth column in my data?
Please attach your file with data (small part of him).
Mario
Mario 2016년 10월 14일
Yes, thank you.
f = fopen('test_1.csv');
c = textscan(f,repmat('%s ',1,129),'delimiter',',','headerlines',1);
fclose(f);
[~,ii] = sort(str2double(regexp(c{6},'\d+','match','once')));
out = cellfun(@(x)x(ii),c(1:end-1),'un',0);
Mario
Mario 2016년 10월 14일
Thank you very much for your help, Andrei Bobrov, this is great!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2016년 10월 14일

댓글:

2016년 10월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by