skip repeated line in text file

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