Skill with regexprep?

조회 수: 2 (최근 30일)
Mike
Mike 2020년 5월 15일
댓글: Mike 2020년 5월 15일
I am new to MATLAB but have years of experience in Perl.
Can anyone offer suggestions as to how to improve the following MATLAB code?
Although what I have works, are there better ways to loop over the record to get closer to the one-line construction of Perl?
My thanks, in advance, for advice and tutorials!
The requirement is to replace all occurrences of a repeat construction "N*Value" with N repeats of the Value. This is part of the import routine for existing data files.
A Perl construction can do this in a single line. In Perl: s!(\d+)\*(\S+)!"$2 " x $1!ge;
An example data record:
DXV
30*60 30*40 30*20 30*10 30*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 10*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 10*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 10*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 10*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 10*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 10*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 10*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 10*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 10*5
3 2 1 0.5 0.3 0.15 0.08 0.04 0.02 5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3
30*5 30*10 30*20 30*40 30*60
/
My current code in MATLAB works OK, but is much clumsier:
record=join(file);
[startIndex,endIndex]=regexp(record,'(\d+)\*(\S+)');
if(~isempty(startIndex))
for n=numel(startIndex):-1:1
tokenNames=regexp(extractBetween(record,startIndex(n),endIndex(n)),'(?<count>\d+)\*(?<value>\S+)','names');
x_record=strings(str2double(tokenNames.count),1);
x_record(:)=tokenNames.value;
record=replaceBetween(record,startIndex(n),endIndex(n),join(x_record));
end
end
  댓글 수: 2
the cyclist
the cyclist 2020년 5월 15일
Can you upload a MAT file with the data record, so that we can experiment without needing to guess exactly how it is stored?
Mike
Mike 2020년 5월 15일
Ah. Didn't know that I could do that. Is the attached what you'd need?
I saved the workspace from within the function after removing extraneous material. Input is file (and key). Output is record. The extra two lines at the end of the code to complete the formatting of the record are:
record=split(record);
record=record(1:end-1);
Again, my thanks!

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

채택된 답변

Stephen23
Stephen23 2020년 5월 15일
편집: Stephen23 2020년 5월 15일
This should get you started:
>> str = '5*0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 30*5 30*10 30*20 30*40 30*60';
>> fun = @(n,c)repmat(sprintf(' %s',c),1,str2double(n)); % or JOIN rather than SPRINTF.
>> str = regexprep(str,'\s*(\d+)\*(\S+)','${fun($1,$2)}')
str =
0.02 0.02 0.02 0.02 0.02 0.02 0.04 0.08 0.15 0.3 0.5 1 2 3 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60
If you use join then you can get rid of the leading \s*.
  댓글 수: 1
Mike
Mike 2020년 5월 15일
Thanks for the rapid assist, and the link.
Mike

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by