explanation for script containing while loop

I was given a script for reading data from a specific file, and although the script works, I don't understand one section of it, it reads as follows:
fid = fopen(Folder);% open file to read
fseek(fid,0,-1);% set read position to beginning of file,
Linechk = strcmp(fgetl(fid),'*END*');% read in line 1
while Linechk == 0
Linechk = strcmp(fgetl(fid),'*END*');% go through lines until '*END*'
end
The main aim here is to find the line where the string END appears, following the appearance of END the data is stored. The question I have is that Linechk is equal to 0 initially (i.e. before the loop), then in following the loop it is equal to 1. I can't understand why it returns 1, surely the while loop only repeats when Linechk is equal to 0 therefore how does the condition remain true when it equals 1?
Entire code:
fid = fopen(Folder);% open file to read
fseek(fid,0,-1);% set read position to beginning of file,
% fseek(fileID, offset, origin).
Linechk = strcmp(fgetl(fid),'*END*');% read in line 1
while Linechk == 0
Linechk = strcmp(fgetl(fid),'*END*');% go through lines until '*END*'
end
n = 1;
while 1;
tline = fgetl(fid);% read in line
if ~ischar(tline), break, end;% if end of file, break and finish
data(n,:) = sscanf(tline,'%f');% put numbers in a matrix (in columns)
n = n+1;
end
fclose(fid);% close file

 채택된 답변

Wayne King
Wayne King 2012년 11월 6일

1 개 추천

I'm not sure I understand your question. Presumably, the first fgetl() call does not obtain the string END, so the string compare is 0. Then the while loop continues the string comparison until the string is END at which time the value of Linechk goes to 1.
Once you exit the loop the value of Linechk is 1. Or perhaps better stated, Linechk equal to 1 keeps the while loop from being evaluated.

댓글 수: 4

Richard
Richard 2012년 11월 6일
편집: Richard 2012년 11월 6일
So does fgetl continue until END is found? I guess I cant understand how fgetl looks at each individual line of the file without specifying the number of lines that the file has. For example, if I knew the file had 10 lines I thought I would have to loop through each individual line to find the line that contained END? The entire code has been added.
fgetl() reads one line of the file. strcmp() compares that one line to '*END*'. If the comparison fails then that line was not '*END*' and the loop needs to be repeated, reading one more line, testing it, etc.. When the comparison works then you are at the '*END*' line.
Richard
Richard 2012년 11월 6일
If I've understood this correctly, does that mean that following the while loop containing Linechk, the script will only be implemented for the remaining lines? For example, if Linechk is at line 9, then following this loop the script will perform some operation for line 10 and so on?
In such a situation, the fgetl() after that would fetch line 10, the one after that would fetch line 11, and so on.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2012년 11월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by