How can we split a line into two lines after n numbers or words?
1 view (last 30 days)
Show older comments
Anjana Krishnan
on 8 Feb 2018
Edited: Walter Roberson
on 8 Feb 2018
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 Comments
Accepted Answer
Walter Roberson
on 8 Feb 2018
Edited: Walter Roberson
on 8 Feb 2018
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 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!