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

2 次查看(过去 30 天)
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 (-------).

采纳的回答

C B
C B 2021-10-10
编辑:C B 2021-10-10
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'}

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by