필터 지우기
필터 지우기

skip repeated line in text file

조회 수: 1 (최근 30일)
Vahideh Ansari Hosseinzadeh
Vahideh Ansari Hosseinzadeh 2021년 8월 5일
답변: Akshat Gupta 2021년 8월 12일
I have a txt file with a repeated pattern as follows, (3 lines to be skipped, 4 lines information to be read, ...)
00-00:00:17.170 : Starting Dose with 1000 cycles
00-00:00:17.228 : Starting Pump motor.
00-00:00:17.328 : Stopping Motor
Dose Cycle = 1
Dosing Time (ms) = 5700
Start Pressure = 0.08
End Pressure = 0.00
00-00:00:17.170 : Starting Dose with 1000 cycles
00-00:00:17.228 : Starting Pump motor.
00-00:00:17.328 : Stopping Motor
Dose Cycle = 2
Dosing Time (ms) = 5800
Start Pressure = 0.98
End Pressure = 0.00
.
.
.
My current code only reads 1 block,
fid = fopen('nums1.txt');
for k = 1:3
tline = fgets(fid);
end
while ischar(tline)
C = textscan(fid, '%s %f', 'Delimiter','=');
tline = fgets(fid);
end
how can I repeat it for next blocks (I have about 400 blocks)? I want to get a results as this:
C{2} = 1
5700
0.08
0.00
2
5800
0.98
0.00
Does anyone have any suggestion?I'd appreciate that.

답변 (1개)

Akshat Gupta
Akshat Gupta 2021년 8월 12일
Following is a possible solution for your usecase:
fid = fopen('nums1.txt');
tline = fgets(fid);
counter=1
while ischar(tline)
if(mod(counter,7)>=4)
if(counter==4)
C = (textscan(tline, '%s %f', 'Delimiter','='));
else
C = [C ; textscan(tline, '%s %f', 'Delimiter','=')];
end
end
counter=counter+1;
tline = fgets(fid);
end
% C(:,2) will return a cell array containing the required result

카테고리

Help CenterFile Exchange에서 Marine and Underwater Vehicles에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by