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

4 views (last 30 days)
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 (-------).

Accepted Answer

C B
C B on 10 Oct 2021
Edited: C B on 10 Oct 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'}

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!