How can we split a line into two lines after n numbers or words?
조회 수: 8 (최근 30일)
이전 댓글 표시
If we have: lw = '27.3025 26.8384 27.3938 0.0000 26.9670 26.8788 26.0803 26.6281 27.1217 0.0000 27.4502' and I want to have: l1 = '27.3025 26.8384 27.3938 0.0000 26.9670', (having 5 numbers and l2 = '26.8788 26.0803 26.6281 27.1217 0.0000 27.4502', the remaining part of the line. Can this be done in a couple of lines in Matlab (not by taking each number/word at a time and forming the two lines)?
댓글 수: 0
채택된 답변
Walter Roberson
2018년 2월 8일
편집: Walter Roberson
2018년 2월 8일
lw = '27.3025 26.8384 27.3938 0.0000 26.9670 26.8788 26.0803 26.6281 27.1217 0.0000 27.4502'
lwords = regexp(lw, '\s+', 'split');
l1 = strjoin(lwords(1:5), ' ');
l2 = strjoin(lwords(6:end), ' ');
or
lw = '27.3025 26.8384 27.3938 0.0000 26.9670 26.8788 26.0803 26.6281 27.1217 0.0000 27.4502'
tokens = regexp(lw, '(?<l1>(\S+\s+){5})(?<l2>.*)', 'names');
l1 = tokens.l1;
l2 = tokens.l2;
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!