How to use extractBetween command?

조회 수: 26 (최근 30일)
Ivan Mich
Ivan Mich 2022년 5월 28일
편집: per isakson 2022년 5월 29일
Hello,
I have a question about a command I want to use. First I have an input .txt value with many lines. From all the line of the input file I would like to separate the lines that between lines
*** SUMMARY OF DATA ***
and
*** END OF SUMMARY ***
I am attaching the input file
I am using these commands:
yy=(regexp(fileread('input.txt'), '\r?\n', 'split')) .';
mid = extractBetween(string(yy),"*** SUMMARY OF DATA ***","*** END OF SUMMARY ***")
but command window shows me
345×0 empty string array
and does not give me the lines between these phrases I would like.
Could you please help me?

채택된 답변

per isakson
per isakson 2022년 5월 28일
편집: per isakson 2022년 5월 29일
startStr and endStr must be in the same row (i.e. in a contiguous sequence of characters).
%%
chr = fileread('input.txt');
mid = extractBetween( chr,"*** SUMMARY OF DATA ***","*** END OF SUMMARY ***");
str = strsplit( mid{1}, '\r\n' );
str(1) = []; % empty row because of leading \r\n
str(end) = []; % empty row because of ending \r\n
reshape( str, [],1 )
ans = 17×1 cell array
{' # of points of data 9 = 5' } {' # of points of data 8-9 = 3' } {' # of points of data 8 = 4' } {' # of points of data 7-8 = 9' } {' # of points of data 7 = 1' } {' # of points of data 6-7 = 3' } {' # of points of data 6 = 2' } {' # of points of data 5-6 = 10' } {' # of points of data 5 = 6' } {' # of points of data 4-5 = 1' } {' # of points of data 4 = 6' } {' # of points of data 3-4 = 0' } {' # of points of data 3 = 0' } {' # of points of data 2-3 = 0' } {' # of points of data 2 = 0' } {' # of points rec/no data = 0' } {' # of points not rec = 0'}

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 5월 28일
Do not do the regexp split until after the extraction

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by