reading specific lines in the text file.

2 vues (au cours des 30 derniers jours)
sermet
sermet le 21 Avr 2015
Modifié(e) : Stephen23 le 21 Avr 2015
I need to read "# / TYPES OF OBSERV" lines and extract the lines' data except "# / TYPES OF OBSERV". For example in the data.txt file (attached file), related lines are;
10 L1 A2 L5 C1 C2 P2 C5 S1 L2# / TYPES OF OBSERV
S5 # / TYPES OF OBSERV
I need to read these two lines and create below cellarray
data_cell={'10' 'L1' 'A2' 'L5' 'C1' 'C2' 'P2' 'C5' 'S1' 'L2' 'S5'}

Réponse acceptée

Stephen23
Stephen23 le 21 Avr 2015
Modifié(e) : Stephen23 le 21 Avr 2015
There are multiple ways that this could be achieved, for example regular expressions and regexp:
>> ptn = '# / TYPES OF OBSERV';
>> str = fileread('data.txt');
>> out = regexp(str, ['^[^\n]+(?=',ptn,'\s*$)'], 'match', 'lineanchors');
>> out = regexp([out{:}], '(?<=\s+)\S+', 'match')
out = '10' 'L1' 'A2' 'L5' 'C1' 'C2' 'P2' 'C5' 'S1' 'L2' 'S5'
where the first regexp call locates the required lines of text, and the second call extracts each individual group of characters.

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by