Reading a text file, skip the intermediate text lines and store the numeric numbers

4 visualizzazioni (ultimi 30 giorni)
Hello,
I have a text file which consists several lines of text followed by several line of numbers and so on. I want to skip the text lines and store the lines with numeric data in a matrix. How can I do that?
Text line 1..........
Text line 2.........
Text line 3............
------------------
1 0.233
Text line 1..........
Text line 2.........
Text line 3............
----------------
2 0.123
3 0.234
4 0.345
Text line 1..........
Text line 2.........
Text line 3............
----------------
8 0.346
9 0.123
10 0.222
11 0.223
......
......
and so on
I just want to store the lines that start with numeric number and ignore all the intermediate text lines and the lines of symbol (-------).

Risposta accettata

C B
C B il 10 Ott 2021
Modificato: C B il 10 Ott 2021
fid = fopen('abc.txt');
tline = fgetl(fid);
storedData ={};
while ischar(tline)
if isstrprop(strtrim(tline(1)),'digit')
disp(tline)
storedData{end+1,1}= tline;
end
tline = fgetl(fid);
end
fclose(fid);
storedData
storedData =
8×1 cell array
{'1 0.233' }
{'2 0.123' }
{'3 0.234' }
{'4 0.345' }
{'8 0.346' }
{'9 0.123' }
{'10 0.222'}
{'11 0.223'}

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by