How to parse text to numbers?

조회 수: 2 (최근 30일)
Pete sherer
Pete sherer 2020년 8월 5일
댓글: Pete sherer 2020년 8월 5일
Hi
I have text cell str looking like this? Is there a way to use regexp to convert this matrix?
ttxt = {'No info','1-50.00000','3-100.000','2-2.0000','Free & Unlimited', '1-100.0000;1-0.0000','1-25.000;1-50.000'}';
ttxt =
{'No info' }
{'1-50.00000' }; % one with 50%
{'3-100.000' } % three with 100% each
{'2-2.0000' } % two with 2% each
{'Free & Unlimited' } % three with 0%
{'1-100.0000;1-0.0000'} % two with first one 100% and 2nd one is 0%
{'1-25.000;1-50.000' } % two with first one 25% and 2nd one 50%
The rules are basically:
when the text is 'No info', it means there are no info, so it returns [0 0 0 0]
When the text is 'Free & Unlimited', it returns [3 0 0 0]
For other cases, there are 2 patterns:
  1. first number > 1: 3-100... or 2-2.00, the first number tells number of repeated %. in this case there are 3 100% and 2 2%, respectively. So it should be [3 1 1 1] and [2 0.02 0.02 0]
  2. multiple cell with ';' {'1-25.00;1-50.00'}; this means there are 2 data with first one is 25%, and 2nd one is 50%, so it should return [2 0.25 0.50 0.0]
want the output matrix to be
tdata = [0 0 0 0;
1 0.5 0 0;
3 1 1 1;
2 0.02 0.02 0;
3 0 0 0;
2 1 0 0;
2 0.25 0.5 0];
  댓글 수: 2
Adam Danz
Adam Danz 2020년 8월 5일
Very unclear.
Please explicitly state each rule that translates a row of text to a row of values in the matrix.
Pete sherer
Pete sherer 2020년 8월 5일
The rules are basically:
when the text is 'No info', it means there are no info, so it returns [0 0 0 0]
When the text is 'Free & Unlimited', it returns [3 0 0 0]
For other cases, there are 2 patterns:
  1. first number > 1: 3-100... or 2-2.00, the first number tells number of repeated %. in this case there are 3 100% and 2 2%, respectively. So it should be [3 1 1 1] and [2 0.02 0.02 0]
  2. multiple cell with ';' {'1-25.00;1-50.00'}; this means there are 2 data with first one is 25%, and 2nd one is 50%, so it should return [2 0.25 0.50 0.0]

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

채택된 답변

Stephen23
Stephen23 2020년 8월 5일
편집: Stephen23 2020년 8월 5일
>> ttxt = {'No info';'1-50.00000';'3-100.000';'2-2.0000';'Free & Unlimited';'1-100.0000;1-0.0000';'1-25.000;1-50.000'}
ttxt =
'No info'
'1-50.00000'
'3-100.000'
'2-2.0000'
'Free & Unlimited'
'1-100.0000;1-0.0000'
'1-25.000;1-50.000'
>> fun = @(s)sscanf(s,'%f-%f;',[2,Inf]);
>> out = cellfun(fun,ttxt,'uni',0);
>> idx = ~cellfun(@isempty,out);
>> baz = @(m,n)[n,repelem(m(2,:)./100,m(1,:)),zeros(1,3-n)];
>> foo = @(m)baz(m,sum(m(1,:)));
>> out(idx) = cellfun(foo,out(idx),'uni',0);
>> out(strncmpi(ttxt,'No',2)) = {[0,0,0,0]};
>> out(strncmpi(ttxt,'Fr',2)) = {[3,0,0,0]};
>> mat = vertcat(out{:})
mat =
0 0 0 0
1.0000 0.5000 0 0
3.0000 1.0000 1.0000 1.0000
2.0000 0.0200 0.0200 0
3.0000 0 0 0
2.0000 1.0000 0 0
2.0000 0.2500 0.5000 0
  댓글 수: 1
Pete sherer
Pete sherer 2020년 8월 5일
Thank you very much. Very efficient

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by