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

조회 수: 4 (최근 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에서 Text Data Preparation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by