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

조회 수: 15 (최근 30일)
Bradley Sprinkle
Bradley Sprinkle 2019년 6월 26일
댓글: Star Strider 2019년 6월 26일
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

채택된 답변

Star Strider
Star Strider 2019년 6월 26일
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
Bradley Sprinkle
Bradley Sprinkle 2019년 6월 26일
I updated my matlab and it works. Thank you very much
Star Strider
Star Strider 2019년 6월 26일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by