How can we split a line into two lines after n numbers or words?

1 回表示 (過去 30 日間)
Anjana Krishnan
Anjana Krishnan 2018 年 2 月 8 日
編集済み: Walter Roberson 2018 年 2 月 8 日
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)?

採用された回答

Walter Roberson
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 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by