MATLAB Answers

How to skip lines of Data in a middle of a text file

22 views (last 30 days)
I am trying to analyze a textfile and skip the three lines in the textfile that begin with the character I, V, or D and only read the lines that begin with R. The I,V,D lines are random throughout the document but always appear sequentially. Basically, i want the code to restart after finishing with the last line of data that begins with an R and restart once it sees the next data line that begins with an I. This is my code so far, that will skip the first I,V,D lines that it sees and read and manipulate the R lines but then it stops. Also attached in example of the data
syms X Y z
fid = fopen('extractcdb.txt','r');
count =0;
chr= ('I''V''D''R');
tf= ischar(chr);
for chr = ('I''V''D')
tline = fgets(fid);
continue
end
for chr= ('R')
tline = fgets(fid);
A = textscan(fid, '%*s %*s %*s %*f %*f %*d %*f %*d %*d %d %*d','Delimiter',',');
Ad = cell2mat(A);
B = [diff(Ad)]*1.2;
B =[zeros(length(Ad) - length(B),1);B];
for i = 1
z = 1:708
C = cumsum(B(i+z)) + Ad(i)
end
end

Accepted Answer

Star Strider
Star Strider on 26 Jun 2019
Try this:
fidi = fopen('extractcdb.txt','rt');
k1 = 1;
while ~feof(fidi)
for k2 = 1:3
StepInfo{k1,k2} = fgetl(fidi) % ‘Step Information’ Section Header Lines Cell Array
end
C = textscan(fidi, ['R' repmat('%f',1,10)], 'Delimiter',',', 'CollectOutput',true);
M = cell2mat(C);
if isempty(M) % Empty Matrix Indicates End-Of-File In Case Of Missing ‘End-Of-File’ Indicator
break
end
D{k1,:} = M; % Section Matrices
fseek(fidi, 0, 0);
k1 = k1 + 1;
end
fclose(fidi);
Out = cell2mat(D); % Optional, You May Want To Keep The Segments Separate
This produces ‘D’ as:
D =
4×1 cell array
{712×10 double}
{712×10 double}
{866×10 double}
{854×10 double}
and ‘StepInfo’ as a (5x3) cell array of the section header lines in the event that you need that information.
  14 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!