Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
Anjana Krishnan
Anjana Krishnan le 8 Fév 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)?

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Fév 2018
Modifié(e) : Walter Roberson le 8 Fév 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;

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by