skip repeated line in text file
Show older comments
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.
Answers (1)
Akshat Gupta
on 12 Aug 2021
0 votes
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
Categories
Find more on Geometry and Mesh in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!