フィルターのクリア

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 Center および File ExchangeMarine and Underwater Vehicles についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by