Collecting data, very specific lines, avoiding unecessay lines in between

Hello All,
I have a .txt file contains 894 lines, now I am interested to collect the line just below the STEP TYPE MEASURED RATE LIMIT RESULT line til end of file. There are 22 steps and I want to collect data from 1 to 22 and then save it in a new .txt file. How can I do it, any idea/comments will be very helpful?
'TEST ERROR SAMPLES ERROR ERROR INTERIM'
'STEP TYPE MEASURED RATE LIMIT RESULT'
'1 BCS ACK/NACK 2054 0.00000 10.0000 Inside'
'TEST ERROR SAMPLES ERROR ERROR INTERIM'
'STEP TYPE MEASURED RATE LIMIT RESULT'
'2 BCS ACK/NACK 2054 2.00000 10.0000 Inside'
.
.
.
.
'TEST ERROR SAMPLES ERROR ERROR INTERIM'
'STEP TYPE MEASURED RATE LIMIT RESULT'
'22 BCS ACK/NACK 2054 1.10000 10.0000 Inside'

Answers (1)

Just use fopen, fgetl, and fclose. Read the line with fgetl(). Then use strfind(). If you find 'TEST' or 'STEP', ignore that line. Then use textscan to get the data from the "good" line. Use fprintf() to write out the data to the new file.

1 Comment

clc;
clear all;
textFilename = 'Test.txt';
FiletoOpen=fopen(textFilename,'r');
s={};
tline = fgetl(FiletoOpen);
while ischar(tline)
s=[s;tline];
tline = fgetl(FiletoOpen);
X=strmatch('STEP',s);
% I dont think last 3 lines is correct, giving me something different. I have already spotted the lines by strmatch ('STEP',s) and now incremeted +1 which is the line I want to read and store but I dont think it's right-any comments?
for k=1:(X+1)
Data{k}= textread(textFilename, '%s', 'delimiter', '\n');
end
end

This question is closed.

Tags

No tags entered yet.

Asked:

MJ
on 4 Oct 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!