Reading a text file, skip the intermediate text lines and store the numeric numbers
1 次查看(过去 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 (-------).
0 个评论
采纳的回答
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'}
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!